Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
842 views
in Technique[技术] by (71.8m points)

node.js - express throws error as `body-parser deprecated undefined extended`

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...