bodyparser的作用

来源:互联网 发布:剑三藏剑成男捏脸数据 编辑:程序博客网 时间:2024/05/18 05:13

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