Search code examples
node.jsexpressexpress-4

Express 4.0 request data received format


I'm using express 4.0+ and this is my code

var express = require('express');
app = express();
var bodyParser = require('body-parser');

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());

app.listen(SERVER_LISTEN_PORT_TWO);

app.use(function (req, res, next) {
  console.log(req.body);
});

On the frontend side, I am sending:

JSON.stringify( {"test":"message"} );

The req.body data printed on the backend side is:

{
  {"test":"message"} : ''
}

I was using just nodejs' createServer function beforehand and the request data would come in as

{"test":"message"}

My question is, does express automatically json-encodes/stringifies the incoming data ( it seems like it ) and how would I be able to disable that? I know I can just not encode the data that I sending from the front-end but I'm not certain why I should change that.


Solution

  • You're not sending the correct Content-Type in your request. If you are sending a JSON payload, the Content-Type needs to be application/json and not application/x-www-form-urlencoded.