body-parser解析POST请求体
POST请求的请求体,包含多种类型,需要解析之后,才能使用。express提供了中间件body-parser用于解析POST请求的请求体,其它用于解析请求体的模块有body
、co-body
。
POST请求,请求体(body)的内容,常见的类型有json、表单、字符串、二进制数据、文件。 分别对应请求头中的请求体类型(Content-Type)为:application/json
、application/x-www-form-urlencoded
、text/plain
、application/octet-stream
、multipart/form-data
。 body-parser
为前4种请求体准备了解析模块,分别为json()
、urlencoded()
、text()
、raw()
。 请求体为文件的,body-parser
没有做处理,如果要解析这种请求体,使用以下模块:busboy
and connect-busboy
,multiparty
and connect-multiparty
,formidable
,multer
。
POST请求的请求体类型及编码,在请求头的Content-Type中指定。
请求体(body)、请求头中的请求体类型(Content-Type)、解析模块、解析模块中的type都对应时,body-parser
也能解析成功。
安装body-parser
$ npm install body-parser [-g]
基本使用
准备工作
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
// create application/json parser
const jsonParser = bodyParser.json();
// create application/x-www-form-urlencoded parser
const urlencodedParser = bodyParser.urlencoded({ extended: false });
// create text/plain parser
const textParser = bodyParser.text();
// create application/octet-stream
const rawParser = bodyParser.raw();
统一指定解析方式
第一种使用方法,是为所有请求统一指定解析方式,不推荐这种方式。
app.use(jsonParser);
app.use(urlencodedParser);
逐个指定解析方式
另一种方法,是为每个POST请求,分别指定解析方式,推荐这种方式。
app.post('url', xxxParser, (req, res) => {});
使用数据
解析成功后,就可以使用req.body
或req.on('data', (chunk) => {}).on('end', () => {});
来获取、使用body中的数据。
POST报文
POST /test HTTP/1.1
Host: 127.0.0.1:3000
Content-Type: text/plain; charset=utf8
Content-Encoding: gzip
chyingp
Content-Type: 请求报文主体的类型、编码。常见的类型有:text/plain
,application/json
,application/x-www-form-urlencoded
,multipart/form-data
。常见的编码有utf-8
,gbk
。
Content-Encoding: 声明报文主体的压缩格式,常见有gzip
,deflate
,identity
。
报文主体: chyingp
。
options
body-parser
提供的4种解析方法,都有options参数,具体含义参考官方文档。