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 } })));
而後就大功告成了!!!!