Following a Twilio Tutorial to Record Phone Calls: Resulting in a 404 HTTP Error - node.js

I'm following this tutorial linked here which incorporates Twilio Programmable Voices and Express to record inbound phone calls. I followed the tutorial exactly, set up a ngrok public server, connected it to my http://localhost:3000, and I get the following output when I navigate to my ngrok link:
Cannot GET /
When I check ngrok in my terminal, it says "404 Not Found" for a GET (which is not in my code) and POST request. If I add a simple GET request to my code (such as "Hello World"), then in the browser, I see "Hello World" when I run my application. In the terminal, GET is able to succeed but POST still returns a "404 Not Found".
I've checked several other stack overflow questions, I've tried importing body-parser as recommended and adding these two lines:
const bodyParser = require('body-parser');
app.use(bodyParser.json());
For reference, my directory has a record.js file, package.json and package-lock.json files, and necessary node modules. In my Twilio Console, I did set up a web hook for my Twilio # to the ngrok link which looks like this: http://39b62a0c34b1.ngrok.io. To show my order of executions:
Run ./ngrok http 3000 in Mac Terminal
Run node record.js in VS Code terminal
Navigate to http://39b62a0c34b1.ngrok.io
Call my Twilio #
The voice message tells me that there's a problem with my application. I've checked the Twilio Debugger and I receive back a 11200 Error informing me that my ngrok link returned a "HTTP status code of 404".
I'm quite new to Node.js, Express, and Twilio, so any and all help is appreciated (please simplify your explanation)! I want to be a Developer Evangelist for Twilio someday so I'm more than prepared to make mistakes now to troubleshoot them in the future.

Twilio uses x-www-form-urlencoded, not JSON, for Webhhooks.
You will need to add Express middleware to parse urlencoded data out.
The 3rd Line:
// Body Parser Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
Where do I find the specifications for inbound HTTP requests to my server?
Let me know how it goes.
Still not clear why you are getting a 404 - Not Found. The app.post should kick in for POST operations. app.all for all operations. Are you using the /record path in your testing?

Related

AWS lambda api gateway with node.js express returns an error of "net::ERR_CONTENT_DECODING_FAILED 200"

The error occurs when my client sends a GET request to the node.js server hosted with AWS lambda api gateway. The server is expected to send back an array of objects with res.json({}). The weird part is that when i test with a response of object or an array of objects with lesser variables in it, it works. I have also tried to JSON.stringify() the array in the server side and JSON.parse() in the client but to no avail. Thanks in advance to everyone helping me and do guide me along as its my first time posting on StackOverflow.
Edit: However i have tried it with a curl command to the endpoint and it returns the json array without any errors.
Alright everyone i found which part of my code caused the bug!
app.use(busboy());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(busboyBodyParser());
As i was playing around with file transfer before with the npm package 'Busboy', imported and used some packages associated with it. However, i forgotten to comment out these codes and thus, i believe it messed up the response body and got the error. Just to be clear this was in the node.js backend.

I have a route that works when I use localhost:3000, but breaks when I use the live app on gcloud's app engine?

The route uses the DELETE method, which I was able to use in nodejs with the use of the method-override npm package. I can use curl for my GET and POST routes fine, but for the DELETE route in particular, when I change the url from http://localhost:3000/ to http://nameofapp.appspot.com/ in my cURL call, I get a "Error 400 (Bad Request)!!1" in return.
I looked at the route on the dashboard, and it's shown under "client errors" and it shows an error rate of 100% but doesn't give me any more information than that. It is always successful with localhost.
Any ideas??

Facebook chatbot post callback doesn't have the correct data structure in nodejs

I've implemented a webhook for a facebook chatbot with php using laravel and all works fine, when I message my bot I receive a post request with expected data structure and I manage to have all working well. Then I was trying to do the webhook implementation using nodejs but when I message my bot the post request that I receive is not the one it would be expected. This is kind of weird because I was able to validate the webhook with the token. I have used the same facebook app and page that I used for the php implementation so I don't think the problem is there. Here's the code in node: http://pastebin.com/0GQcXdV2
I would expect the request structure to be: http://pastebin.com/GFU89LjA
but instead it's this: http://pastebin.com/51S7DrkG
I'm sorry if this question seems stupid and I'm missing something obvious but can't figure out what. I'm kind of new to node js so maybe this is a newbie mistake, but if anyone can tell me what am I doing wrong it would be very appreciated. Thanks in advance
I managed to solve my problem by importing npm body-parser and make my express app use it for returning JSON. According to the npm documentation the bodyParser object provides middleware factories that expose the body of the request and assign it to req.body in plain text, json , raw or url encoding form body (https://www.npmjs.com/package/body-parser). To solve my problem i just added the following two lines of code:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
More information on body parser can be found here.

Syntax error in loopback api gateway

I am following a tutorial :http://strongloop.com/strongblog/node-js-loopback-api-gateway-sample-applications/
and I am getting an error in the code underneath the Proxy heading in the link mentioned above.
Code against the error is
var proxy = require('./middleware/proxy');
var proxyOptions = require('./middleware/proxy/config.json');
app.use(proxy(proxyOptions));
{
"rules": [
"^/api/(.*)$ http://localhost:3002/api/$1 [P]"
]
}
the error message is
is there any thing wrong with the json?
It looks like you just copied and pasted the code from that blog post, but that was not intended to be runnable code, it was just a snippet to illustrate that you could proxy requests. I think the blog post could be a lot more clear (I'll ping the author of it separately). That said, we have an example project which has some actual code that shows you how you might proxy actual requests.
If you're just trying to get a basic example working, then you probably don't even need those lines in your application JS file, so I'd just remove them entirely.
If you do need to proxy requests, take a look at the example project I linked to above, specifically the proxy middleware they have in it.
My Problem is solved the problem was that I did not run the API Server http://localhost:3002 where the request was to be sent. I was only running the proxy server whereas there should be an API Server running also to which the request was to be sent.

Socket.io + Express CORS Error on localhost (not allowed by Access-Control-Allow-Origin)

I have a working node.js Express server to which I would to add socket.io support (allow javascript clients to connect via socket.io). I can connect to the express server via a Javascript $.get(), but the socket.io.connect() command fails due to a CORS error.
My testing machine is OSX with Apache to serve the client, thus port 80 is taken, so I have node.js/express running on port 8888. I added socket.io per the documentation:
var exp = express();
var server = require('http').createServer(api.server);
exp.listen(8888);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket) {
console.log('connection');
});
I properly see "info: socket.io started" in my node.js logs.
Then, on the client, I attempt to connect to the server...
this.socket = io.connect('http://localhost:8888');
this.socket.on('connect',function() {
socket.emit('install','test');
});
However, I'm getting a CORS error in the console in Chrome:
XMLHttpRequest cannot load http://localhost:8888/socket.io/1/?t=1358715637192. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
HOWEVER, THIS works fine!
$.get('http://localhost:8888',function(e,d){
console.log(e,d);
});
So I double checked my headers, for both localhost:8888 and localhost -- both are properly returning the headers which (should) allow for the cross-domain requests...
Access-Control-Allow-Origin: *
Any ideas?
CORS is a very tricking thing to get working (or at least it was for me). I recommend this resource here: http://enable-cors.org/
Following what they do very carefully helped me. I also found that different browsers gave different visibility over the CORS request/responses which helped.
I found that Chrome was easier to get working than firefox, but firefoxes tools such as firebug, were quite nice to work with.
My gut feel from your information is that you might need your request to have an X-Request-With in your request attributes.
I also found using fidler to send the http requests allowed me to narrow my problems down to the server side initially and get that working. You will find browser enforce CORS, but something like fidler doesn't and thus provides another way of inspecting what is happening.
I definately recommend trying to break the problem in half so that you can see if it is server side or client side that is not behaving how you expect.
My problem was related to returning the same CORS response for the OPTIONS header as the POST or GET. That was wrong. Chrome allowed it. Firefox didnt. Any options request that is sent out will be sent out once, then in the future it will be cached and not resent (Which caused alot of confusion for me initially). For the options request you just need a standard response saying its ok to proceed, then in the post or get response i believe you want your cors responses there only.

Resources