Node.js (with express & bodyParser): unable to obtain form-data from post request Node.js (with express & bodyParser): unable to obtain form-data from post request express express

Node.js (with express & bodyParser): unable to obtain form-data from post request


In general, an express app needs to specify the appropriate body-parser middleware in order for req.body to contain the body.

[EDITED]

  1. If you required parsing of url-encoded (non-multipart) form data, as well as JSON, try adding:

    // Put this statement near the top of your modulevar bodyParser = require('body-parser');// Put these statements before you define any routes.app.use(bodyParser.urlencoded());app.use(bodyParser.json());

    First, you'll need to add body-parser to the dependencies property of your package.json, and then perform a npm update.

  2. To handle multi-part form data, the bodyParser.urlencoded() body parser will not work. See the suggested modules here for parsing multipart bodies.


To handle multipart/form-data request that support file upload, you need to use multer module. npm link for multer middleware


I followed thishttps://www.tutorialspoint.com/expressjs/expressjs_form_data.htm

var bodyParser = require('body-parser');var multer = require('multer');var forms = multer();// apply themapp.use(bodyParser.json());app.use(forms.array()); app.use(bodyParser.urlencoded({ extended: true }));// how to userouter.post('/', function(req, res) {    console.log(req.body);    console.log('received the widget request');});