koa-bodyParse 接受post 大表单报错:nodejs Error request entity too large

  Node.js从2009横空出世以后,至今已经7年有余,各类web框架也林林总总,目前大约在npm上有35万左右包,刨去前端和一些无心义的封装,也是有很是可观的优秀的模块的。其中web框架也是特别抢眼的,从早期的express到如今koa,对异步流程控制的改进前仆后继。随着移动端崛起面向api的框架hapi和restify也如火如荼,更有一些面向特性的框架,好比thinkjs对es6/es7/typescript支持,总体来讲,质量都是很是不错的,算百花齐放,仍是那句话,即便不优化,你也能用这些框架得到较高的性能。

  本公司技术转型,尝试用nodejs 做为web-server,做为一名phper的我,对“php是世界上最好的语言”深信不疑,却又不得不放下执念,纵身跳入node学习的苦海当中,本觉得苦海无涯悬崖勒马,然柳暗花明,得以窥探node的博大精深之处,越陷越深不能自拔。因此本文主要记录下我的在学习使用node过程当中遇到的各类问题和所填的坑。
  
  koa 基于nodeJs 平台的下一代web开发框架
  koa 是由 Express 原班人马打造的,致力于成为一个更小、更富有表现力、更健壮的 Web 框架。 
  koa-bodyParse 解析body 的中间件,用以接受post 过来的表单,json数据,或者上传的文件流。
  如下是配置项说明:
  
  • 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
  }
})));

  而后就大功告成了!!!!

相关文章
相关标签/搜索