npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

post-bodyparser

v1.0.3

Published

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

Downloads

1

Readme

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!'
})