I’m sending the following JSON string to my server.
(
{
id = 1;
name = foo;
},
{
id = 2;
name = bar;
}
)
On the server I have this.
app.post('/', function(request, response) {
console.log("Got response: " + response.statusCode);
response.on('data', function(chunk) {
queryResponse+=chunk;
console.log('data');
});
response.on('end', function(){
console.log('end');
});
});
When I send the string, it shows that I got a 200 response, but those other two methods never run. Why is that?
I think you’re conflating the use of the
responseobject with that of therequest.The
responseobject is for sending the HTTP response back to the calling client, whereas you are wanting to access the body of therequest. See this answer which provides some guidance.If you are using valid JSON and are POSTing it with
Content-Type: application/json, then you can use thebodyParsermiddleware to parse the request body and place the result inrequest.bodyof your route.Update for Express 4.16+
Starting with release 4.16.0, a new
express.json()middleware is available.Updated for Express 4.0 – 4.15
Body parser was split out into its own npm package after v4, requires a separate install
npm install body-parserFor earlier versions of Express (< 4)
Test along the lines of: