Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm-koa-bodyparser #108

Open
mowatermelon opened this issue Aug 11, 2020 · 0 comments
Open

npm-koa-bodyparser #108

mowatermelon opened this issue Aug 11, 2020 · 0 comments

Comments

@mowatermelon
Copy link
Owner

koa-bodyparser

官方仓库

https://github.com/koajs/body-parser

官方npm地址

依赖包地址

https://www.npmjs.com/package/koa-bodyparser

在线运行地址

https://npm.runkit.com/koa-bodyparser

基础介绍

A body parser for koa, based on co-body. support json, form and text type body.

基于co-body的 koa body解析器。支撑json,form 和text键入主体。

Notice: this module don't support parsing multipart format data, please use co-busboy to parse multipart format data.
注意:此模块不支持解析多部分格式数据,请使用co-busboy解析多部分格式数据。

基础安装

npm install koa-bodyparser --save   # npm
yarn add koa-bodyparser             # Yarn

基础使用

(function (log) {
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
 
const app = new Koa();
app.use(bodyParser());
 
app.use(async ctx => {
  // the parsed body will store in ctx.request.body
  // if nothing was parsed, body will be an empty object {}
  ctx.body = ctx.request.body;
});
})(console.log)

参数说明

参数名 参数说明
enableTypes parser will only parse when request type hits enableTypes, support json/form/text/xml, default is ['json', 'form'].
encoding requested encoding. Default is utf-8 by co-body.
formLimit limit of the urlencoded body. If the body ends up being larger than this limit, a 413 error code is returned. Default is 56kb.
jsonLimit limit of the json body. Default is 1mb.
textLimit limit of the text body. Default is 1mb.
xmlLimit limit of the xml body. Default is 1mb.
strict when set to true, JSON parser will only accept arrays and objects. Default is true. See strict mode in co-body. In strict mode, ctx.request.body will always be an object(or array), this avoid lots of type judging. But text body will always return string type.
detectJSON custom json request detect function. Default is null.
extendTypes support extend types
onerror support custom error handle, if koa-bodyparser throw an error, you can customize the response like
disableBodyParser you can dynamic disable body parser by set ctx.disableBodyParser = true.

detectJSON

custom json request detect function. Default is null.

app.use(bodyparser({
    detectJSON: function(ctx) {
        return /\.json$/i.test(ctx.path);
    }
}));

extendTypes

support extend types:

app.use(bodyparser({
    extendTypes: {
        json: ['application/x-javascript'] // will parse application/x-javascript type body as a JSON string
    }
}));

onerror

support custom error handle, if koa-bodyparser throw an error, you can customize the response like:

app.use(bodyparser({
    onerror: function(err, ctx) {
        ctx.throw('body parse error', 422);
    }
}));

相关网站

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant