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...');
});
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.
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.
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.
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.
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}));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With