bodyparser

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

首先科普下bodyparser的作用:它用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理。

现在继续说下上面截图问题的意思:意为

因为新版的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
 
上面是我遇到问题,在慕课问答中找到答案总结的,希望能给大家带来帮助。
原文地址:https://www.cnblogs.com/JennyLin77/p/6128030.html