Reading AJAX post variables in Node.JS (with Express) Reading AJAX post variables in Node.JS (with Express) express express

Reading AJAX post variables in Node.JS (with Express)


The req object you have there has no body property. Have a look at http://expressjs.com/api.html#req.body:

This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {} when bodyParser() is used.

So, you need to add the bodyParser middleware to your express webapp like this:

var app = express();app.use(express.bodyParser());


The issue was indeed resolved by including the bodyParser middleware as suggested by thejh.

Just make sure to visit the url provided in that answer as to visit Express updated specification: http://expressjs.com/api.html#req.body

The documentation provides this example (Express 4.x):

var app = require('express')();var bodyParser = require('body-parser');var multer = require('multer'); app.use(bodyParser.json()); // for parsing application/jsonapp.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencodedapp.use(multer()); // for parsing multipart/form-dataapp.post('/', function (req, res) {  console.log(req.body);  res.json(req.body);})

For this to work the body-parser module needs to be installed separately:

https://www.npmjs.com/package/body-parser