PayloadTooLargeError:请求实体太大

时间:2018-05-12 09:33:24

标签: javascript node.js http express

我在bodyParser中遇到以下错误:

PayloadTooLargeError: request entity too large
at readStream (/root/server/node_modules/raw-body/index.js:155:17)
at getRawBody (/root/server/node_modules/raw-body/index.js:108:12)
at read (/root/server/node_modules/body-parser/lib/read.js:77:3)
at urlencodedParser (/root/server/node_modules/body-parser/lib/types/urlencoded.js:116:5)
at Layer.handle [as handle_request] (/root/server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/root/server/node_modules/express/lib/router/index.js:317:13)
at /root/server/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/root/server/node_modules/express/lib/router/index.js:335:12)
at next (/root/server/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/root/server/node_modules/body-parser/lib/types/json.js:118:7)
at Layer.handle [as handle_request] (/root/server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/root/server/node_modules/express/lib/router/index.js:317:13)
at /root/server/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/root/server/node_modules/express/lib/router/index.js:335:12)
at next (/root/server/node_modules/express/lib/router/index.js:275:10)
at initialize (/root/server/node_modules/passport/lib/middleware/initialize.js:53:5)
at Layer.handle [as handle_request] (/root/server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/root/server/node_modules/express/lib/router/index.js:317:13)
at /root/server/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/root/server/node_modules/express/lib/router/index.js:335:12)
at next (/root/server/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/root/server/node_modules/body-parser/lib/types/json.js:118:7)

代码正在关注

const express = require("express");
const bodyParser = require('body-parser');

function setEntity(req, res) {
   // something....
}

module.exports = (app) => {

  const router = new express.Router();

  app.use(bodyParser.json({limit:'50mb'}));
  app.use(bodyParser.urlencoded({
    extended: true
  }));

  router.use('/set/', (req, res) => {
    setEntity(req, res);
  });

  return router;
};

它似乎与this question

类似

所以我尝试了这三种解决方案。

1

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

2

app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));

3

app.use(bodyParser.json({limit:1024*1024*20, type:'application/json'}));
app.use(bodyParser.urlencoded({extended:true,limit:1024*1024*20,type:'application/x-www-form-urlencoding' }));

但结果没有改变。

另一个设置如下

nginx has this setting : client_max_body_size 200M;
data size is under 500kb (json).
express : 4.15.3
body-parser : 1.18.2

我不知道为什么我无法更改限制数据大小。

3 个答案:

答案 0 :(得分:3)

我的问题是因为我有app.use(express.json())并且

app.use(bodyParser.json({ limit: "50mb" }))

app.use(bodyParser.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 }))

我删除了app.use(express.json())后解决了它。希望它对遇到同样问题的人有所帮助。

答案 1 :(得分:0)

谢谢它真的有效

app.use(bodyParser.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 }))

我在删除 app.use(express.json()) 后解决了它。希望对遇到同样问题的人有所帮助。

答案 2 :(得分:0)

您不再需要 bodyParser:

// Instead of using body-parser middleware, use the new Express implementation
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb', extended: true, parameterLimit: 50000}));