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?
json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.
To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
Express doesn't automatically parse the HTTP request body for you, but it does have an officially supported middleware package for parsing HTTP request bodies. As of v4. 16.0, Express comes with a built-in JSON request body parsing middleware that's good enough for most JavaScript apps.
I think you're conflating the use of the response
object with that of the request
.
The response
object is for sending the HTTP response back to the calling client, whereas you are wanting to access the body of the request
. 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 the bodyParser
middleware to parse the request body and place the result in request.body
of your route.
Update for Express 4.16+
Starting with release 4.16.0, a new express.json()
middleware is available.
var express = require('express'); var app = express(); app.use(express.json()); app.post('/', function(request, response){ console.log(request.body); // your JSON response.send(request.body); // echo the result back }); app.listen(3000);
Updated for Express 4.0 - 4.15
Body parser was split out into it's own npm package after v4, requires a separate install npm install body-parser
var express = require('express') , bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.json()); app.post('/', function(request, response){ console.log(request.body); // your JSON response.send(request.body); // echo the result back }); app.listen(3000);
For earlier versions of Express (< 4)
var express = require('express') , app = express.createServer(); app.use(express.bodyParser()); app.post('/', function(request, response){ console.log(request.body); // your JSON response.send(request.body); // echo the result back }); app.listen(3000);
Test along the lines of:
$ curl -d '{"MyKey":"My Value"}' -H "Content-Type: application/json" http://127.0.0.1:3000/ {"MyKey":"My Value"}
For Express v4+
install body-parser from the npm.
$ npm install body-parser
https://www.npmjs.org/package/body-parser#installation
var express = require('express') var bodyParser = require('body-parser') var app = express() // parse application/json app.use(bodyParser.json()) app.use(function (req, res, next) { console.log(req.body) // populated! next() })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With