Promise风格的bodyparser,接收request对象做为参数,返回解析后的body对象,返回结果前做了charset转换,解压缩,内容编码转换等处理
bodyparser实现原理解析
包含对代码设计的详细注解
Promise风格的bodyparser,接收request对象做为参数,返回解析后的body对象,返回结果前做了charset转换,解压缩,内容编码转换等处理
npm i body-parser-promise
http.createServer(async (req,res) => {
// 如果Content-Type为application/www-form-urlencode或者application/json,则body是解析后的对象
// 如果Content-Type为text/plain,则body是一个字符串
// 具体的内容输出参考下面的demo
const body = await getRequestBody(req, res);
})
# Demo
如果你想看看这个demo是怎么运作的,你可以
1. clone项目
```git
git clone https://github.com/penghuwan/body-parser-promise.git
npm install
node client.js // Node.js客户端,向server发送gzip压缩后的消息
node server.js // Node.js服务端,负责解析每个请求的request.body,并输出
// 来自client.js客户端发送的压缩body
我是一个被Gzip压缩后的数据
// 来自前端请求,连续三次fetch的body
{
data: '我是彭湖湾',
contentType: 'application/json',
charset: 'gbk'
}
{
data: '我是彭湖湾',
contentType: 'application/x-www-form-urlencoded',
charset: 'UTF-8'
}
我是彭湖湾,这句话采用UTF-8格式编码,content-type为text/plain