Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use body-parser in express router?

I have a post api that has object but i am not able to print in console its throowing undefined i thought i am missing body-parser but after adding body parser i see error body-parser deprecated bodyParser: use individual json/urlencoded middlewares Any help will be appreciated.

routes.js

var express = require('express');
var bodyParser = require('body-parser');
var Diagram = require('./api/diagram/diagram.controller');
var router = express.Router();
router.post('/saveUpdateDiagram',bodyParser,function(req,res){
    console.log(req.body);
});

app.js

var express = require('express');
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
var router = express.Router();
var route = require('./server/routes').router;
var mongoose = require('mongoose');
mongoose.connection.on('connected', function() {
    console.log('MongoDB connected ');
});


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


app.listen(8760, function() {
    console.log('I am listening 8760...');
});
like image 431
hussain Avatar asked Oct 11 '16 19:10

hussain


People also ask

Does Express include body parser?

Express body-parser is an npm module used to process data sent in an HTTP request body. It provides four express middleware for parsing JSON, Text, URL-encoded, and raw data sets over an HTTP request body.

Do you need to install body parser with Express?

body-parser doesn't have to be installed as a separate package because it is a dependency of express version 4.16. 0+. body-parser isn't a dependency between version 4.0. 0 and 4.16.

Why do we need body parser in Express?

In order to get access to the post data we have to use body-parser . Basically what the body-parser is which allows express to read the body and then parse that into a Json object that we can understand.

How do I get a body from Express request?

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.


2 Answers

Your use of body-parser in app.js is fine. It is middleware, and it is loaded with app.use so that it will be applied to every incoming request.

You can remove it in routes.js, so that it looks like so:`

var express = require('express');
var Diagram = require('./api/diagram/diagram.controller');
var router = express.Router();
router.post('/saveUpdateDiagram', function(req,res){
    console.log(req.body);
});

` Also, try replacing:

app.use(bodyParser.urlencoded({
    extended: false
}))

with:

app.use(bodyParser.urlencoded({extended: true}));
like image 77
WindUpDurb Avatar answered Oct 09 '22 21:10

WindUpDurb


It means that using the bodyParser() constructor has been deprecated, as of 2014-06-19.

 app.use(bodyParser()); //Now deprecated You now need to call the methods separately
 app.use(bodyParser.urlencoded());

 app.use(bodyParser.json()); //And so on.

If you're still getting a warning with urlencoded you need to use

 app.use(bodyParser.urlencoded({
     extended: true
 }))

The extended config object key now needs to be explicitly passed, since it now has no default value, as said over here.

like image 30
Dani Avatar answered Oct 09 '22 20:10

Dani