216

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: '[email protected]', password: 'pass' } // i am getting response

Can any help me to understand this issue please?

3
  • 1
    @RamprasathSelvam - Yes, It was!! Commented Oct 4, 2018 at 11:40
  • @RamprasathSelvam - may something wrong with your code, version of module or integration, please check Commented 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. Commented Jul 29, 2020 at 6:37

13 Answers 13

442

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 }))
Sign up to request clarification or add additional context in comments.

3 Comments

@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'}}})
@BaileyParker is your comment related to the extended:true expression? I think that the expression is nested.
For its meaning, see: \ node.js - What is the meaning of "bodyParser.urlencoded({ extended: true }))" and "bodyParser.json()" in Express.js? - Stack Overflow \ stackoverflow.com/questions/55558402/…
83

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 Comments

so, no need of npm i body-parser ?
That is correct, do not install body-parser, it's already included
21

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

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

Comments

13

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);
});

Comments

8

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.

Comments

8

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 Comments

bodyParser is depriciated still getting this warning
This helped me, I was confused because after removing body-parser from my app I was still seeing this error.
@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
3

If you're using Node v16.xx.x and "express": "^4.17.x", there is no need again to use bodyParser. 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 }));

Comments

2

If you are using node version v13.12.0:

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

2 Comments

Today's date has no bearing on what version of Node someone is using.
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!
2

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

app.use(express.json());

Comments

1

You don't need to use body-parser anymore.

In the new version of express.js, this option of parsing is available.

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

Comments

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

Comments

0

I found this error recently and wanted to add some solution which I found. The error body-parser deprecated undefined extended: provide extended option

If you're using body-parser:

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

For Express 4.16.0+ (no need for body-parser):

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

PS: Express library now includes urlencoded and json middleware, so body-parser is unnecessary.

Comments

-2

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
      }));

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.