1

I have the following setup in my app.js:

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
var router = express.Router();
require('./routes/index')(router);
app.use(router);

In my routes/index.js I have all the routes defined:

module.exports = function (router) {
    /* GET home page. */
    router.get('/', function(req, res, next) {
        res.render('index', { title: 'Home' });
    });
    router.post('/', function(req, res, next) {
        console.log(req.body);
    });
}

Then in my app entry point bin/server.js:

var app = require('../app');
var debug = require('debug')('NodeJSDemo:server');
var http = require('http');
var port = 3000;
app.set('port', port);
var server = http.createServer(app);
server.listen(port);

When I make a POST call on http://localhost:3000/ with a request body, in the console log console log request body is undefined.

Is there anything wrong with my setup? From this post Express.js req.body undefined it seems as long as I call

app.use(bodyParser.json())

before loading routes, it should be fine but seems like it does not.

Community
  • 1
  • 1
as3rdaccount
  • 3,711
  • 12
  • 42
  • 62
  • Try adding this line before app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); – davidev Jan 01 '16 at 21:20
  • No luck :( This should be something trivial, not sure why express does not support them built-in. – as3rdaccount Jan 01 '16 at 21:27
  • I recommend to generate an express app using express generator, have a look here: http://expressjs.com/en/starter/generator.html – davidev Jan 01 '16 at 21:33
  • Actually that is the exact same directory structure I have, the only thing I changes is instead of calling the start script www.js, I renamed to server.js – as3rdaccount Jan 01 '16 at 21:35
  • How are you making POST requests to your server? Specifically, what are you setting the `Content-Type` to be for those requests? Note that bodyParser doesn't handle requests with `Content-Type` set to `multipart/form-data`: https://www.npmjs.com/package/body-parser – rmacqueen Apr 03 '17 at 21:08

1 Answers1

0

The problem arises from what type of resource you are sending on your POST request.

Your bodyParser.json() is ONLY parsing json format. Or in other words if you simply POST a simple form that in most cases defaults to application/x-www-form-urlencoded you will not get the body object.

As it is stated in the documentation:

Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option.

and

type - The type option is used to determine what media type the middleware will parse. Defaults to application/json.

The solution would be to implement and cather from other scenarios so:

application/x-www-form-urlencoded

app.use(bodyParser.urlencoded())

multipart/form-data

Express does not parse multipart bodies as stated in the documentation:

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

  1. busboy and connect-busboy
  2. multiparty and connect-multiparty
  3. formidable
  4. multer

For Express versions: +4.17.0

You could not include the bodyParser dependency. And use the express built-in methods as:

express.json()   
express.text() 
express.urlencoded()

They are built based on the bodyParser module so instead calling bodyParser.json(). You would do express.json() and achieve the same results.

Source: https://expressjs.com/en/resources/middleware/body-parser.html#bodyparserurlencodedoptions

iwaduarte
  • 1,600
  • 17
  • 23