8

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...');
});
hussain
  • 6,587
  • 18
  • 79
  • 152
  • 1
    Try taking the `bodyParser` you are passing into the route out. Since you are using the bodyParser at the server level (via `app.use(...)`) you already have the middleware loaded. – Dave V Oct 11 '16 at 19:49

4 Answers4

8

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}));
WindUpDurb
  • 116
  • 1
  • 5
  • i removed body-parser from routes.js ,but i am still getting undefined printed in console no idea why – hussain Oct 11 '16 at 20:03
  • See if setting extended to true works, per the example in my edits. – WindUpDurb Oct 11 '16 at 20:09
  • 3
    i removed body-parser from app and added to router and used same as app `router.use(bodyParser.urlencoded({ extended: false }))' it worked for me – hussain Oct 11 '16 at 20:20
3

Your use of body-parser in app.js is fine. It is middleware, and it is loaded with app.use but the problem is the position where you used the app.use('/', route); it should be placed below the app.use(bodyParser.urlencoded({ extended: true }));

Prajith P
  • 49
  • 2
2

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.

peteb
  • 18,552
  • 9
  • 50
  • 62
Dani
  • 2,448
  • 8
  • 27
  • 44
0

Simple method, Install body-parser using following command

npm install -g body-parser

Then require it in your application using

var bodyParser = require("body-parser"); 

then you can use it in your app adding following line of code

app.use(bodyParser.urlencoded({extended: true}));
NaturalCoder
  • 128
  • 9