title: Can't get POST body from request using Express.js
body: <p>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 <code>req</code> object, but I get an error when trying to access <code>req.body</code> </p>
  
  <p>Here's what I have: </p>
  
  <pre><code>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'));     
  });
  </code></pre>
  
  <p>On the <code>console.log(JSON.stringify(req.body));</code> I get <code>undefined</code> and on the <code>res.send(req.body.self);</code> I get <code>TypeError: Cannot read property 'self' of undefined</code></p>
  
  <p>I've been seaching for this type of error and usually the issue is that people do not include <code>app.use(express.bodyParser());</code> middleware , so I also tried using <code>app.use(express.urlencoded());</code> and <code>app.use(express.json());</code>, which didn't work either.</p>
  
  <p>If I do a <code>console.log(req)</code> I can see the entire object but I do not get to see <code>body</code> or any of the content I'm passing when doing the POST request from a client (I'm passing it as JSON).</p>
  
  <p>**I know I could use <code>restify</code> or <code>sails.js</code> to build APIs within Node but i want to do everything myself so I can learn from the experience.*</p>
  
  <p>Thanks</p>
  
  <p>EDIT:
  I had to put the <code>bodyparser</code> middleware before the <code>app.router</code> middleware, that fixed it!</p>