一尘不染

错误:请求实体太大

node.js

我收到express的以下错误:

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。我的express.js中有以下使用声明

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

在提琴手中,我可以看到content-length标头,其值为:1078702

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

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


阅读 189

收藏
2020-07-07

共1个答案

一尘不染

我最近遇到了相同的错误,发现的所有解决方案均无效。

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

当添加一个console.log('Limit file size: '+limit);in
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

我们可以看到,首先,在加载connect模块时,限制设置为1mb(1048576字节)。然后,当我设置限制时,console.log再次调用,这次限制为52428800(50mb)。但是,我仍然得到一个413 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

这意味着在某种程度上,它会connect重置limit参数,而忽略我们指定的内容。我尝试bodyParser在路由定义中分别指定参数,但也没有运气。

虽然我找不到任何永久设置它的正确方法,但是您可以直接在模块中“ 修补 ”它。如果您使用的是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上打开了一个有关此问题的问题。

[编辑-找到解决方案]

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

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

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

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

[第二次编辑]

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

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

如果extended未为明确定义该选项bodyParser.urlencoded(),则会抛出警告(body-parser deprecated undefined extended: provide extended option)。这是因为此选项在下一版本中将是必需的,并且 不再
是可选的。有关更多信息extended选项,可以参考自述body-parser

[第三编辑]

似乎从Express v4.16.0起,我们可以回到执行此操作的初始方法(感谢@GBMan的提示):

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