body-parser 是一个Http请求体解析中间件

Posted 太阳花0525

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了body-parser 是一个Http请求体解析中间件相关的知识,希望对你有一定的参考价值。

1.这个模块提供以下解析器

  (1) JSON body parser (2) Raw body parser (3)Text body parser (4)URL-encoded form body parser

2.如何安装 

  npm install body-parser

3.API

  var bodyParser = require(‘body-parser‘);

  可以通过body-parser对象创建中间件,当接受到客户端请求时所有的中间件都会给req.body添加属性,请求内容为空时,解析为空或者错误。

  (1)bodyParser.json(options)

    处理json数据

    app.use(bodyParser.json());

  (2)bodyParser.raw(options)

    处理Buffer数据流

  (3)bodyParser.text(options)

    处理文本数据

  (4)bodyParser.urlencoded(options)   

    处理UTF-8的编码的数据

    app.use(bodyParser.urlencoded({extented:false}))    

    extend

      ture->使用queryString库(默认) false->使用qs库。

以上是关于body-parser 是一个Http请求体解析中间件的主要内容,如果未能解决你的问题,请参考以下文章

Express定制参数解析错误响应值

[转] Nodejs 进阶:Express 常用中间件 body-parser 实现解析

multer 解析文件上传

Node+MongoDB数据接口的开发

node的express框架,核心第三方模块body-parser 获取我们所有post请求传过来数据

用Express重写之前的留言本案例