Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get POST body from request using Express.js

I'm working on an API with NodeJS and Express (and more stuff like mongo, sockets, etc) but i'm stuck on a very simple step I believe. I'm just trying to get the information from the POST req object, but I get an error when trying to access req.body

Here's what I have:

var express     = require('express'),
    http        = require('http'),
    path        = require('path'),
    fs          = require('fs'),
    io          = require('socket.io');
    dynroute    = require('dynroute');

var app = express();
app.set('port', process.env.PORT || 3999);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(app.router);
app.use(express.bodyParser());

app.post('/user', function(req, res) {

    console.log(JSON.stringify(req.body));
    res.send(req.body.self);
});

http.createServer(app).listen(app.get('port'), function ()
{
    console.log('App Server is now running at:' + app.get('port'));     
});

On the console.log(JSON.stringify(req.body)); I get undefined and on the res.send(req.body.self); I get TypeError: Cannot read property 'self' of undefined

I've been seaching for this type of error and usually the issue is that people do not include app.use(express.bodyParser()); middleware , so I also tried using app.use(express.urlencoded()); and app.use(express.json());, which didn't work either.

If I do a console.log(req) I can see the entire object but I do not get to see body or any of the content I'm passing when doing the POST request from a client (I'm passing it as JSON).

**I know I could use restify or sails.js to build APIs within Node but i want to do everything myself so I can learn from the experience.*

Thanks

EDIT: I had to put the bodyparser middleware before the app.router middleware, that fixed it!

like image 311
kevinblanco Avatar asked Dec 04 '13 16:12

kevinblanco


People also ask

How do I get the request body in express?

Express has a built-in express. json() function that returns an Express middleware function that parses JSON HTTP request bodies into JavaScript objects. The json() middleware adds a body property to the Express request req . To access the parsed request body, use req.

Why is req body undefined in express?

Looks like the body-parser is no longer shipped with express. We may have to install it separately. var express = require('express') var bodyParser = require('body-parser') var app = express() // parse application/x-www-form-urlencoded app.


4 Answers

Move the bodyParser middleware above the router middleware!

var app = express();
app.set('port', process.env.PORT || 3999);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(app.router);
like image 99
Renato Gama Avatar answered Oct 18 '22 22:10

Renato Gama


Content-Type = "application/json" should be one of the Request Headers

like image 43
Aakash Avatar answered Oct 18 '22 20:10

Aakash


BodyParser is no longer bundled with Express

npm install body-parser
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
like image 4
Tobias Artz Avatar answered Oct 18 '22 20:10

Tobias Artz


For me @kevinblanco's note of "I had to put the bodyparser middleware before the app.router middleware, that fix it!" did it for me. Putting it in a separate answer since its buried at the bottom.

Your code will need to look something like this:

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// My routing here
like image 1
Ash Blue Avatar answered Oct 18 '22 22:10

Ash Blue