203

In my node app, I am using express. all works fine, But i am getting error in the cmd. I use all are updated modules...

my code :

var express = require('express');
var bodyParser = require('body-parser');
var jade = require('jade');
var app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));


app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded()); // to support URL-encoded bodies


app.get('/',function(req,res){
    res.render('index.jade');
});

app.get('/login',function(req,res){
    res.render('index.jade');
});

app.post('/login',function(req,res){
    console.log(req.body);
});

app.get('/signup',function(req,res){
    res.render('signup.jade');
});

var env = process.env.PORT || 3000;


app.listen(env, function(req, res){
    console.log('i am working!');
});

Error:

D:\myLogin>node app
body-parser deprecated undefined extended: provide extended option app.js:11:20 //why i am getting this?
i am working!
{ username: 'jbarif@gmail.com', password: 'pass' } // i am getting response

Can any help me to understand this issue please?

3
  • 1
    @RamprasathSelvam - Yes, It was!!
    – 3gwebtrain
    Oct 4, 2018 at 11:40
  • @RamprasathSelvam - may something wrong with your code, version of module or integration, please check
    – 3gwebtrain
    Oct 4, 2018 at 12:39
  • 2
    Where do we vote for the worst error message ever? Especially considering this is highly likely to happen, the message just looks like a bag of random words.
    – spechter
    Jul 29, 2020 at 6:37

11 Answers 11

411

You have to explicitly set extended for bodyParser.urlencoded() since the default value is going to change in the next major version of body-parser. Example:

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

Since express 4.16.0, you can also do:

app.use(express.urlencoded({ extended: true }))
2
  • 6
    @SamarthAgarwal According to the readme, it uses the qs module to parse the body which allows for a nested array like syntax to be parsed such as test[foo][bar]=baz (which becomes {'test': {'foo': {'bar': 'baz'}}}) Jul 11, 2015 at 3:35
  • @BaileyParker is your comment related to the extended:true expression? I think that the expression is nested.
    – Timo
    Mar 27, 2021 at 9:41
77

Attention: With express version => 4.16.0 the body-parser middleware was added back under the methods express.urlencoded() and express.json()

Which can be used as:

app.use(express.urlencoded({extended: true})); 
app.use(express.json());   
2
  • 2
    so, no need of npm i body-parser ? Jun 30, 2021 at 10:55
  • 2
    That is correct, do not install body-parser, it's already included
    – David
    Sep 1, 2021 at 12:05
19

The error says you need to provide the extended option for the body-parser like so:

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

Don't use body-parser

It's now built-in with new versions of Express, you can access request body just like this only using express:

app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads

Hence, you can now uninstall body-parser using npm uninstall body-parser



To get the POST content, you can use req.body

app.post("/yourpath", (req, res)=>{

    var postData = req.body;

    //Or if this doesn't work

    var postData = JSON.parse(req.body);
});
7

If you facing 'bodyParser' is deprecated.

Just do

app.use(express.urlencoded({extended: true})); 
app.use(express.json());

Note: If you are using 4.16.0 or later express version.

7

As from Express version 4.16.0, you're expected to pass in extended property inside the bodyParser.urlencoded()

//  parse JSON-encoded bodies and URL-encoded bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

See npm.js documentation page for sample: https://www.npmjs.com/package/body-parser#expressconnect-top-level-generic

If you're using Node v16.xx.x and "express": "^4.17.x" and above, there is no need again to use bodyPerser. It is now incorporated with express by default. Simply do the following below:

//  parse JSON-encoded bodies and URL-encoded bodies
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
3
  • bodyParser is depriciated still getting this warning Mar 19, 2021 at 10:07
  • This helped me, I was confused because after removing body-parser from my app I was still seeing this error.
    – robm
    Jan 20, 2022 at 2:54
  • 1
    @ZainUlAbidin you don't need Body-Parser anymore inasmuch as you're using a version of Node v16.xx.x and Express: ^4.17.x
    – tMan44wiz
    Mar 31 at 16:07
1

If you are using node version v13.12.0:

app.use(express.urlencoded({ extended: true }))
2
  • Today's date has no bearing on what version of Node someone is using.
    – Ghostrydr
    Feb 19, 2021 at 5:11
  • This is the only approach that works. Sadly, I saw it in very few blog posts, forums, etc., and the amount of confusion regarding this is through the roof!
    – ankush981
    Mar 17, 2021 at 9:31
1

You don`t need "body-parser" explicitly installed now. This will work

app.use(express.json());
1

If you're using Node v16.xx.x and "express": "^4.17.x", there is no need again to use bodyPerser. It is now incoporated with Express by default. Simply do the following below:

//  parse JSON-encoded bodies and URL-encoded bodies
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
0

if you are here after christmas from 2020 you just have to put the midlawares in order before your express declaretion and after the routes declarationenter image description here

-1

Set limit 50 MB for avoid data handling error., In urlencode limit 50mb is for help you to pass imageData throw url

  app.use(bodyParser.json({
        limit : '50mb'    ///////// LIMIT for JSON
      }));

    app.use(bodyParser.urlencoded({
        limit : '50mb', ///////// LIMIT for URL ENCODE (image data)
        extended : true
      }));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.