首页 > Web开发 > 详细

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

时间:2018-03-19 16:17:18      阅读:1915      评论:0      收藏:0      [点我收藏+]
  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‘].

  • encode: 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.

  • 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.

    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);
      }
    }));

     

  • disableBodyParser: you can dynamic disable body parser by set ctx.disableBodyParser = true. 

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
  }
})));

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

    

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

原文:https://www.cnblogs.com/tieshengdetie/p/8601849.html

(0)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!