enableTypes: parser will only parse when request type hits enableTypes, default is ['json', 'form']
.javascript
encode: requested encoding. Default is utf-8
by co-body
.php
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
.java
textLimit: limit of the text
body. Default is 1mb
.node
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.git
detectJSON: custom json request detect function. Default is null
.es6
app.use(bodyparser({ detectJSON: function (ctx) { return /\.json$/i.test(ctx.path); } }));
extendTypes: support extend types:github
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:web
app.use(bodyparser({ onerror: function (err, ctx) { ctx.throw('body parse error', 422); } }));
disableBodyParser: you can dynamic disable body parser by set ctx.disableBodyParser = true
. typescript
app.use(async (ctx, next) => { if (ctx.path === '/disable') ctx.disableBodyParser = true; await next(); }); app.use(bodyparser());
nodejs Error request entity too large
var koaBody = require('koa-bodyParse')({ "formLimit":"5mb", "jsonLimit":"5mb", "textLimit":"5mb" });
可是所有试事后,卵用没有,仍是报错,因而开始追koa-bodyParse代码,终于发现问题所在:
koa-bodyParse 依然依赖 co-body,raw-body,并用qs 模块来处理url中的参数(post 提交也会序列化成标准化url),因而查询qs 模块的官方文档找到这么一句话:
qs 模块是能够配置解析字符串时的参数个数的,上图中opts.queryString 只是一个默认的空对象,也就是默认的参数为1000个,当你的表单过大超过这个1000时,不管你怎么配置koa-bodyParse 都是没用的 ,因而解决方案以下,在你的node入口文件引用koa-bodyParse的地方增长配置:
app.use(convert(bodyParser({ enableTypes:['json', 'form', 'text'], formLimit:"3mb", queryString:{ parameterLimit:100000000000000 } })));
而后就大功告成了!!!!