为了账号安全,请及时绑定邮箱和手机立即绑定

出现"Most middleware (like bodyParser) is no longer bundled with Express"错误怎么解决

命令行里选定指定路径然后输入 node app.js 

会提示:

Error: Most middleware (like bodyParser) is no longer bundled with Express and m

ust be installed separately. Please see https://github.com/senchalabs/connect#mi

ddleware.

    at Function.Object.defineProperty.get (E:\dcspace\node\imooc\node_modules\ex

press\lib\express.js:89:13)

    at Object.<anonymous> (E:\dcspace\node\imooc\app.js:8:17)

    at Module._compile (module.js:456:26)

    at Object.Module._extensions..js (module.js:474:10)

    at Module.load (module.js:356:32)

    at Function.Module._load (module.js:312:12)

    at Function.Module.runMain (module.js:497:10)

    at startup (node.js:119:16)

    at node.js:906:3


然后去他给的这个github上安装了connect()中间件,为什么还是无法启动,还是报这样的错呢

正在回答

4 回答

Returns middleware that only parses urlencoded bodies. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.

A new body object containing the parsed data is populated on the request object after the middleware (i.e.req.body). This object will contain key-value pairs, where the value can be a string or array (when extended isfalse), or any type (when extended is true).

The urlencoded function takes an option options object that may contain any of the following keys:

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). The "extended" syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded. For more information, pleasesee the qs library.


0 回复 有任何疑惑可以回复我~

bodyParser.urlencoded 中设置 extended 为 true 和为 false 有什么区别吗?

0 回复 有任何疑惑可以回复我~
#1

根号三

求解.竟然没法追加提问?╮(╯▽╰)╭
2015-04-28 回复 有任何疑惑可以回复我~
#2

夏不语冰 回复 根号三

可以把 右上角有只编辑笔
2017-08-21 回复 有任何疑惑可以回复我~
#3

蝴蝶是我_我就是蝴蝶

bodyParser 中间件,可以将post数据转成js对象, 要求请求头的content-type属性是 application/x-www-form-urlencoded 或者application/json true或者false应该是这个区别
2018-11-06 回复 有任何疑惑可以回复我~
按照楼上大哥 说的倒是能跑通了,但是后面提交数据的时候需要将 extended: false 改为
extended: true
否则会报错:
TypeError: Cannot read property '_id' of undefined

相关资料:
通常 POST 内容的格式是 application/x-www-form-urlencoded, 因此要用下面的方式来使用:
app.use(require('body-parser').urlencoded({extended: true}))
见:
https://github.com/expressjs/body-parser


2 回复 有任何疑惑可以回复我~

首先翻译这句英文,大概的意思就是新版的express中已经不包含bodyparser了,那就需要大家单独安装bodyparser,安装命令是npm install body-parser,然后在app.js中加载body-parser模块var bodyParser = require('body-parser'),把app.use(express.bodyParser())替换成app.use(bodyParser.urlencoded({ extended: false })),这样调试就没问题了。

12 回复 有任何疑惑可以回复我~
#1

stono

老师好! 按着老师说的改了,的确过了;
2015-01-28 回复 有任何疑惑可以回复我~
#2

James_dai

恩,确实有用!
2016-11-24 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
node+mongodb 建站攻略(一期)
  • 参与学习       91892    人
  • 解答问题       889    个

带你完整实现一个从前端到后端的项目,初中级前端开发工程师必学课程

进入课程

出现"Most middleware (like bodyParser) is no longer bundled with Express"错误怎么解决

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信