今天在用bodyparser时,遇到了以下问题:git

首先科普下bodyparser的做用:它用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理。github
如今继续说下上面截图问题的意思:意为express
由于新版的express中不包含bodyparser,须要咱们单独安装bodyparser。
解决方案:
使用npm install body-parser安装body-parser,而后在app.js中加载body-parser模块var bodyParser = require('body-parser'),把app.use(express.bodyParser())替换成app.use(bodyParser.urlencoded({extended:false})),这样就ok了。
可是还有一点要注意,在用post提交数据时须要把参数extended:false改成extended:true,不然会报错。
为啥会报错呢,由于一般post内容的格式为application/x-www-form-urlencoded,所以要用下面的方式来使用:app.use(require('body-parser').unlencoded({extended:true}))
详情见https://github.com/expressjs/body-parser
上面是我遇到问题,在慕课问答中找到答案总结的,但愿能给你们带来帮助。