express throws error as `body-parser deprecated undefined extended`

node.jsExpress

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

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

Solution 2 - node.js

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

Solution 3 - node.js

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

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

Solution 4 - node.js

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.

Solution 5 - node.js

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

Solution 6 - node.js

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

Solution 7 - node.js

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

Solution 8 - node.js

If you are using node version v13.12.0:

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

Solution 9 - node.js

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

app.use(express.json());

Solution 10 - node.js

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

Solution 11 - node.js

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question3gwebtrainView Question on Stackoverflow
Solution 1 - node.jsmscdexView Answer on Stackoverflow
Solution 2 - node.jsDhiral KaniyaView Answer on Stackoverflow
Solution 3 - node.jsmfreitasView Answer on Stackoverflow
Solution 4 - node.jsHappy PatelView Answer on Stackoverflow
Solution 5 - node.jsAbrahamView Answer on Stackoverflow
Solution 6 - node.jstMan44wizView Answer on Stackoverflow
Solution 7 - node.jstMan44wizView Answer on Stackoverflow
Solution 8 - node.jsSüŽan BhattaraiView Answer on Stackoverflow
Solution 9 - node.jsAmit BaderiaView Answer on Stackoverflow
Solution 10 - node.jsDario CoronelView Answer on Stackoverflow
Solution 11 - node.jsGowtham SooryarajView Answer on Stackoverflow