I am using the express module in node.js and I am trying to read the body of an HTTP GET request and send an answer back to the user based on the content of the body. I am new to node.js and this is not working for me.
I know I should be using HTTP POST for this (and it works when I am using post), but the server I am trying to mimic in node.js uses GET, so I have to stick to it. Is there a way to do this in node.js? Thanks!
Here is a sample code I did. When I use app.post, I see the logs for the body, but when I use app.get, I see no logs at all!
app.get('/someURL/', function(req, res){
var postData = '';
req.on('data', function(chunk) {
console.log("chunk request:");
console.log(chunk);
postData += chunk;
});
req.on('end', function() {
console.log("body request:");
console.log(postData);
});
//Manipulate response based on postData information
var bodyResponse = "some data based on request body"
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.setHeader('Content-Length', bodyResponse.length);
res.send(bodyResponse);
};