https://github.com/koajs/bodyparser
https://www.npmjs.com/package/koa-bodyparser
413 Payload Too Large
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status/413
响应状态码 413 Payload Too Large 表示请求主体的大小超过了服务器愿意或有能力处理的限度,
服务器可能会(may)关闭连接以防止客户端继续发送该请求

  1. yarn add koa-bodyparser@2

413 Request Entiry Too large

post 提交数据的时,bodyparser 限制了大小问题?
request entity too large PayloadTooLargeError: request entity too large
status code:413 Request Entiry Too large
原因:
上传文件失败,报上面的错:请求实体太大
https://stackoverflow.com/questions/52783959/nest-js-request-entity-too-large-payloadtoolargeerror-request-entity-too-larg

midway

midway 使用的 bodyparser 是纯粹的 koa-bodyparser,支持配置,在 config/config.default.ts 中

  1. exports.bodyParser = {
  2. formLimit: '5000kb',
  3. jsonLimit: '5000kb', // 设置为 5MB大小
  4. };

koa-bodyparse默认值

  1. exports.bodyParser = {
  2. enableTypes: ['json', 'form', 'text', 'xml'],
  3. encoding: '',
  4. formLimit: '56kb', // 默认值
  5. jsonLimit: '1mb',
  6. textLimit: '1mb',
  7. xmlLimit: '1mb',
  8. strict: true,
  9. detectJSON: null
  10. }
  11. // detectJSON
  12. app.use(bodyparser({
  13. detectJSON: function (ctx) {
  14. return /\.json$/i.test(ctx.path);
  15. }
  16. }));
  • strict: true 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.

express

express4.0之后的版本

  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. app.use(bodyParser.json({limit: '10mb'}));
  4. app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));

express4.0之前的版本

  1. app.use(express.json({limit: '10mb'}));
  2. app.use(express.urlencoded({limit: '10mb'}));

https://gist.github.com/Maqsim/857a14a4909607be13d6810540d1b04f
https://stackoverflow.com/questions/19917401/error-request-entity-too-large

nest.js

  1. import * as bodyParser from 'body-parser';
  2. import { AppModule } from './app.module';
  3. async function bootstrap() {
  4. const app = await NestFactory.create(AppModule);
  5. app.useStaticAssets(`${__dirname}/public`);
  6. // the next two lines did the trick
  7. app.use(bodyParser.json({limit: '50mb'}));
  8. app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
  9. app.enableCors();
  10. await app.listen(3001);
  11. }
  12. bootstrap();

nginx修改

nginx有个参数client_max_body_size在限制请求实体的大小,把它改大点就好了。
{如果配置文件没有这个参数,应该会在系统里有个默认的值}

  1. 打开nginx配置文件 nginx.conf, 路径一般是:/etc/nginx/nginx.conf。
  2. 在http{}段中加入 client_max_body_size 20m。20m为允许上传的最大值,可自己决定多大。
  3. 保存后重启nginx
  1. vi /etc/nginx/nginx.conf
  2. sudo nano /etc/nginx/nginx.conf
  3. vi /usr/local/nginx/conf/nginx.conf
  4. # set client body size to 2M #
  5. client_max_body_size 2M;

https://www.cyberciti.biz/faq/linux-unix-bsd-nginx-413-request-entity-too-large/