简体   繁体   English

NodeJS/Express 请求实体太大 - Heroku

[英]NodeJS/Express Request Entity Too Large - Heroku

I've looked everywhere and can't seem to find an explanation about this, this is what Heroku is showing in my logs:我到处寻找,似乎找不到对此的解释,这就是 Heroku 在我的日志中显示的内容:

45:45+00:00 app[web.7]: Error: Request Entity Too Large
45:45+00:00 app[web.7]:     at Object.error (/app/node_modules/express/node_modules/connect/lib/utils.js:44:13)
45:45+00:00 app[web.7]:     at Object.limit [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/limit.js:45:47)
45:45+00:00 app[web.7]:     at next (/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
45:45+00:00 app[web.7]:     at Object.logger [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/logger.js:157:5)
45:45+00:00 app[web.7]:     at next (/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
45:45+00:00 app[web.7]:     at Object.favicon [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/favicon.js:78:7)
45:45+00:00 app[web.7]:     at next (/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
45:45+00:00 app[web.7]:     at Object.expressInit [as handle] (/app/node_modules/express/lib/middleware.js:31:5)
45:45+00:00 app[web.7]:     at next (/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
45:45+00:00 app[web.7]:     at Object.query [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/query.js:44:5)
45:45+00:00 app[web.7]: POST /mls 413 1ms - 980

I've included the last log record because I'm not sure if it suggests the HTTP URL where these errors happened, or if it's the URL of the next request the Heroku dyno is processing (we receive hundreds of requests a second so it's a bit crazy to track).我已经包含了最后一条日志记录,因为我不确定它是否暗示了发生这些错误的 HTTP URL,或者它是否是 Heroku dyno 正在处理的下一个请求的 URL(我们每秒收到数百个请求,所以这是一个跟踪有点疯狂)。

My application ( ExpressJS ) requires receiving large POST requests, which is why I've put我的应用程序( ExpressJS )需要接收大量的 POST 请求,这就是我把

app.use(express.limit('5mb'));

at the top of the app to allow large post requests (usually less than 2MB).在应用程序顶部以允许大型发布请求(通常小于 2MB)。 I'm not sure if the errors above are being caused by receiving a request that's too large or trying to send a request to S3 that's too large, or something else.我不确定上述错误是由于收到过大的请求还是尝试向 S3 发送过大的请求或其他原因引起的。 Any ideas?有任何想法吗?

Thanks!谢谢!

if you are using Express v4+ you can do:如果您使用的是 Express v4+,您可以执行以下操作:

app.use(bodyParser.json());
app.use(bodyParser({limit: '5mb'}));

Or you can try the following single line solution, but I haven't personally tested或者您可以尝试以下单行解决方案,但我没有亲自测试过

app.use(bodyParser.json({limit: '1mb'}));

While the solution app.use(bodyParser({limit: '5mb'}));而解决方案app.use(bodyParser({limit: '5mb'})); works in some cases, it won't work well if you have a lot of ampersand & in the content you are trying to upload.在某些情况下工作,如果你有很多的符号,将无法正常工作&在内容你尝试上传。 This is because the body parser will split based on & .这是因为主体解析器将根据&拆分。 The solution that worked for me is to replace all occurrences of & with encodeURIComponent('&') using Regex.对我encodeURIComponent('&')的解决方案是使用正则表达式用encodeURIComponent('&')替换所有出现的& So you have:所以你有了:

contentToUpload = contentToUpload .replace(/&/g, encodeURIComponent('&'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM