Koa-bodyparse Accept post Big form error: Nodejs error request entity too large

Source: Internet
Author: User
Tags parse error

Since 2009, node. JS has been around for more than 7 years, and there are a variety of web frameworks that currently have about 350,000 packages on NPM, planing the front end and some meaningless packages, and there are very impressive modules. The web framework is also particularly eye-catching, from the early express to now KOA, the improvement of asynchronous process Control fought. With the rise of the mobile side of the API-oriented framework HAPI and restify are also in full swing, there are some features-oriented framework, such as THINKJS to Es6/es7/typescript support, overall, quality is very good, count flowers, or that sentence, even if not optimized, You can also use these frameworks to achieve higher performance.

The company's technology transformation, try to use Nodejs as a web-server, as a phper I, "PHP is the best language in the world," convinced, but also have to put down the practice, jump into node learning the abyss of suffering, this thought the abyss of life rebuild, but steady, The depth and breadth of node can be explored, and the deeper it becomes.    So this article mainly records the individual in learning to use node process encountered various problems and filled pits.   KOA's next-generation Web development framework based on the Nodejs platform KOA is built by Express's original squad and is dedicated to becoming a smaller, more expressive, and robust web framework.  Koa-bodyparse parses the body's middleware to accept post-form, JSON data, or uploaded file streams. The following is a description of the configuration item:
  • enabletypes: Parser would only have 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, the JSON parser would only accept arrays and objects. Default is true . See strict mode in co-body . In strict mode, the'll always is a ctx.request.body object (or array), this avoid lots of type judging. But text body would always return string type.

  • Detectjson: Custom JSON request detect function. Default is null .

    app.use  (bodyparser{function  (CTX) {    return /\.json$/i.test ( (Ctx.path);  } ));

  • Extendtypes: Support Extend types:

    App.use (Bodyparser ({  extendtypes: {    json: [//}})  ];

  • onerror: Support custom error handle, if koa-bodyparser throw a error, you can customize the response like:

    App.use (Bodyparser ({  function  (err, CTX) {    ctx. Throw (' Body parse error ', 422);  });

  • Disablebodyparser: Can dynamic disable body parser by set ctx.disableBodyParser = true .

App.use (The async (CTX, next) = = {  iftrue;  await next ();}); App.use (Bodyparser ());
However, in the project development process because of business needs, I need to submit a large form, and then encountered the following error:
Nodejs Error request Entity too large
Then only hope in the strong Baidu, the basic solution is this:
var koabody = require (' Koa-bodyparse ') ({    "formlimit": "5MB",    "Jsonlimit": "5MB",    "Textlimit": "5MB"});

But after all the test, eggs with no, or error, so began to chase Koa-bodyparse code, finally found the problem lies:

  

Koa-bodyparse still relies on Co-body,raw-body and uses the QS module to process the parameters in the URL (the post submission will also be serialized as a standardized URL), so query the official documentation for the QS module to find this sentence:

The QS module can be configured to parse a string when the number of parameters, Opts.querystring is just a default empty object, is the default parameter is 1000, when your table slip larger than this 1000, no matter how you configure Koa-bodyparse is useless, So the solution is as follows: Add the configuration in your node portal where the file refers to Koa-bodyparse.

  

App.use (CONVERT (bodyparser {  enabletypes:[' json ', ' form ', ' text '],  formlimit: "3MB",  querystring:{    parameterlimit:100000000000000  }));

And then it's done!!!!

    

Koa-bodyparse Accept post Big form error: Nodejs error request entity too large

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.