Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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).

My application (ExpressJS) requires receiving large POST requests, which is why I've put

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

at the top of the app to allow large post requests (usually less than 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. Any ideas?

Thanks!

like image 839
user2209729 Avatar asked Mar 26 '13 01:03

user2209729


2 Answers

if you are using Express v4+ you can do:

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'}));
like image 103
microweb10 Avatar answered Oct 22 '22 10:10

microweb10


While the solution 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. So you have:

contentToUpload = contentToUpload .replace(/&/g, encodeURIComponent('&'))
like image 23
Ekundayo Blessing Funminiyi Avatar answered Oct 22 '22 10:10

Ekundayo Blessing Funminiyi