I’m making a simple form in Node.js. Everything else seems to be working correctly, but the function that is supposed to receive post request data is never getting called. Here’s the relevant code snippet:
if (request.method == 'POST') {
var body = '';
console.log(request.body);
request.on('data', function (chunk) {
console.log("got the post request data"); //nothing logged to console
body += chunk;
});
request.on('end', onRequestEnd(body, response));
}
The function onRequestEnd does get called, but later my code breaks when there’s nothing but an empty string in the parameter body. Is the keyword ‘data’ correct?
The code was modified from an answer here: How do you extract POST data in Node.js?. I’ll post more if needed.
After lots of frustration I solved the problem myself!
I changed the line:
to:
It had something to do with callbacks. I’m not exactly sure why this works and the other one doesn’t though. This is how I feel: http://www.masti-xpress.com/images/Story-of-Every-Programmer.jpg