Express中间件之body-parse

来源:互联网 发布:海康网络球机安装教程 编辑:程序博客网 时间:2024/06/05 14:55

新版的express(4.x)中已经不包含bodyparser了,那就需要大家单独安装bodyparser,安装命令是npm install body-parser,然后在app.js中加载body-parser模块var bodyParser =require('body-parser'),然后使用;




解析:

bodyParser中间件用来解析http请求体,是express默认使用的中间件之一。使用express应用生成器生成一个网站,它默认已经使用了 bodyParser.json 与 bodyParser.urlencoded 的解析功能,除了这两个,bodyParser还支持对text、raw的解析。

app.use(bodyParser.raw);

app.use(bodyParser.json);

app.use(bodyParser.urlencoded({

    extended: false

});

bodyParser.json是用来解析json数据格式的。bodyParser.urlencoded则是用来解析我们通常的form表单提交的数据;

常见的四种Content-Type类型:

o    application/x-www-form-urlencoded 常见的form

o    multipart/form-data 文件提交

o    application/json 提交json格式的数据

o    text/xml 提交xml格式的数据

 

 

 


0 0
原创粉丝点击