错误:请求实体太大

2022-08-29 22:34:37

我收到以下快速错误:

Error: request entity too large
    at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/node_modules/raw-body/index.js:16:15)
    at json (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/json.js:60:5)
    at Object.bodyParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.cookieParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.logger (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/logger.js:158:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.staticMiddleware [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
TypeError: /Users/michaeljames/Documents/Projects/Proj/mean/app/views/includes/foot.jade:31
    29| script(type="text/javascript", src="/js/socketio/connect.js")
    30| 
  > 31| if (req.host='localhost')
    32|     //Livereload script rendered 
    33|     script(type='text/javascript', src='http://localhost:35729/livereload.js')  
    34| 

Cannot set property 'host' of undefined
    at eval (eval at <anonymous> (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:152:8), <anonymous>:273:15)
    at /Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:153:35
    at Object.exports.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:197:10)
    at Object.exports.renderFile (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:233:18)
    at View.exports.renderFile [as engine] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:218:21)
    at View.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/application.js:504:10)
    at ServerResponse.res.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/response.js:801:7)
    at Object.handle (/Users/michaeljames/Documents/Projects/Proj/mean/config/express.js:82:29)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:188:17)

POST /api/0.1/people 500 618ms

我正在使用 meanstack。我在表达中具有以下使用声明.js

//Set Request Size Limit
app.use(express.limit(100000000));

在小提琴手中,我可以看到内容长度标题,其值为:1078702

我相信这是八位字节,这是1.0787兆字节。

我不知道为什么express不让我发布我之前在另一个未使用均值堆栈项目结构的express项目中发布的json数组。


答案 1

我最近遇到了同样的错误,我发现的所有解决方案都不起作用。

经过一番挖掘,我发现该设置确实正确设置了限制。app.use(express.bodyParser({limit: '50mb'}));

添加 in 和重新启动节点时,我在控制台中收到以下输出:console.log('Limit file size: '+limit);node_modules/express/node_modules/connect/lib/middleware/json.js:46

Limit file size: 1048576
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Limit file size: 52428800
Express server listening on port 3002

我们可以看到,首先,在加载模块时,限制设置为1mb(1048576字节)。然后,当我设置限制时,再次调用,这次限制52428800(50mb)。但是,我仍然得到一个.connectconsole.log413 Request entity too large

然后我添加了,并在控制台中看到另一行,当使用大请求调用路由时(在错误输出之前):console.log('Limit file size: '+limit);node_modules/express/node_modules/connect/node_modules/raw-body/index.js:10

Limit file size: 1048576

这意味着以某种方式,在某个地方,重置了限制参数并忽略了我们指定的内容。我尝试在路由定义中单独指定参数,但也没有运气。connectbodyParser

虽然我没有找到任何正确的方法来永久设置它,但您可以直接在模块中“修补”它。如果您使用的是 Express 3.4.4,请在 以下位置的第 46 行添加此内容:node_modules/express/node_modules/connect/lib/middleware/json.js

limit = 52428800; // for 50mb, this corresponds to the size in bytes

如果您运行的不是相同版本的 Express,则行号可能会有所不同。请注意,这是不好的做法,如果您更新模块,它将被覆盖

因此,此临时解决方案目前有效,但是一旦找到解决方案(或者模块已修复,以防出现模块问题),您应该相应地更新代码。

我已经在他们的GitHub上打开了一个关于这个问题的问题。

[编辑 - 找到解决方案]

经过一番研究和测试,我发现在调试时,我添加了,但是在.然后,Express 会将全局限制设置为 1mb,因为他在运行脚本时遇到的第一个解析器是 。移动到它上面做了伎俩。app.use(express.bodyParser({limit: '50mb'}));app.use(express.json());express.json()bodyParser

也就是说,该方法将在 Connect 3.0 中弃用,不应使用。相反,你应该显式声明你的解析器,如下所示:bodyParser()

app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));

如果您需要多部分(用于文件上传),请参阅此帖子

[第二次编辑]

请注意,在 Express 4 中,您必须要求使用 body-parser 模块并使用其 and 方法,而不是 and,如下所示:express.json()express.urlencoded()json()urlencoded()

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

如果未为 显式定义该选项,它将引发警告 ()。这是因为此选项在下一个版本中是必需的,并且不再是可选的。有关该选项的详细信息,请参阅 的自述文件extendedbodyParser.urlencoded()body-parser deprecated undefined extended: provide extended optionextendedbody-parser

[第三次编辑]

似乎在Express v4.16.0之后,我们可以回到最初执行此操作的方式(感谢@GBMan提示):

app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));

答案 2

在我的情况下,添加这些行是不够的:

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

我尝试在urlencoded函数上添加参数Limit选项,因为文档说并且错误不再出现。

参数Limit选项控制URL编码数据中允许的最大参数数。如果请求包含的参数多于此值,则将向客户端返回 413。默认值为 1000。

请尝试使用以下代码:

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));