1.0.3 • Published 2 years ago

post-bodyparser v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
2 years ago

bodyParser

基于 Node 的 http POST方法请求体解析,支持 multipart/form-data、application/json、application/x-www-form-urlencoded

安装

依赖 node v7.6.0 及以上

npm install post-bodyparser

使用

const BodyParser = require('post-bodyparser')
const options = {
    uploadpath: __dirname
}
const body = await new BodyParser(req).parse()
  • 参数
    • req: node 的 http.IncomingMessage 类的实例(如果使用koa框架可以用ctx.req获取)
    • options: 可选
      • uploadpath,文件的上传路径,默认是系统的临时目录
      • encoding,http.IncomingMessage 实例执行setEncoding 方法,默认不执行
  • 返回值(body): 一个key-value对象, 如果请求中存在多个相同的name字段, value将被解析为数组。

根据不同的content-type使用不同的api

const BodyParser = require('post-bodyparser')
const parser =  new BodyParser(req);
const contentType = req.headers["content-type"];
  • multipart/form-data
const body = await parser.formData()

对于文件类型的字段,文件上传后将保存在系统的临时目录,body的结构用ts接口描述如下

interface IBody {
    [name: string]: {
        value: string // value是文件的临时路径
        name: string
        filename: string
        contentType: string
    } | string
}
const exampleBody = {
    username: "qoxop",
    picture: {
        value: "/xx/xx/82665854-9f48-4166-a2bb-fdf78cc014b4.xxx.jpg",
        name: "picture",
        filename: "xxx.jpg",
        contentType: "image/jpeg"
    }
}
  • application/json
const body = await parser.json();

将请求体解析为json字符串,并转为对象,结构与请求体的结构一致

  • application/x-www-form-urlencoded
const body = await parser.urlencoded()

将请求体解析为key-value模式的对象

使用koa中间件

中间件将解析结果写入ctx.request.body

const uploadMiddlewareMaker = require('post-bodyparser/koa')

const options = {max: 1024 * 1024 * 10, uploadpath: __dirname}
const needThrow = true; // 若遇到超过限制或不符合的content-type时是否抛出异常
const uploadMiddleware = uploadMiddlewareMaker(options, needThrow);

// 注册 koa 中间件
app.use(uploadMiddleware)
app.use(async (ctx, next) => {
    console.log(ctx.request.body)
    ctx.body = 'upload done!'
})