[Solved-3 Solutions] Error: request entity too large
Error Description:
- We receive the following error with 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
click below button to copy the code. By - JavaScript tutorial - team
Solution 1:
- setting
app.use(express.bodyParser({limit: '50mb'}));does set the limit correctly. - When adding a
console.log('Limit file size:'+limit);
- In ,
node_modules/express/node_modules/connect/lib/middleware/json.js:46and restarting node, we get this output in the console:
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
click below button to copy the code. By - JavaScript tutorial - team
- We can see that at first, when loading the
connectmodule, the limit is set to 1mb (1048576 bytes). Then when we set the limit, theconsole.logis called again and this time the limit is 52428800 (50mb). However, we still get a413 Request entity too large.
- Then added
console.log('Limit file size: '+limit);innode_modules/express/node_modules/connect/node_modules/raw-body/index.js:10and saw another line in the console when calling the route with a big request (before the error output) :
Limit file size: 1048576
click below button to copy the code. By - JavaScript tutorial - team
- This means that somehow, somewhere,
connectresets the limit parameter and ignores what we specified. I tried specifying thebodyParserparameters in the route definition individually, but no luck either.
- While I did not find any proper way to set it permanently, you can "patch" it in the module directly. If you are using Express 3.4.4, add this at line 46 of
node_modules/express/node_modules/connect/lib/middleware/json.js :limit = 52428800; // for 50mb, this corresponds to the size in bytes
- The line number might differ if you don't run the same version of Express. Please note that this is a bad practice and will be overwritten if you update your module.
- So this temporary solution works for now, but as soon as a solution is found (or the module fixed, in case it's a module problem) you should update your code accordingly.
- I have opened an issue on their Github about this problem.
Solution 2:
- After some research and testing, I found that when debugging, I added
app.use(express.bodyParser({limit: '50mb'}));, but afterapp.use(express.json());.Express would then set the global limit to 1mb because the first parser he encountered when running the script wasexpress.json().
- Moving
bodyParserabove it did the trick. - That said, the
bodyParsermethod will be deprecated in Connect 3.0 and should not be used. Instead, you should declare your parsers explicitely, like so :
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));
click below button to copy the code. By - JavaScript tutorial - team
- In case you need multipart (for file uploads) see this post.
Solution 3:
- Note that in Express 4, instead of
express.json()andexpress.urlencoded(),you must require the body-parser module and use itsjson()andurlencoded()methods, like so :
var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
click below button to copy the code. By - JavaScript tutorial - team
- If the
extendedoption is not explicitly defined forbodyParser.urlencoded(),it will throw a warning (body-parser deprecated undefined extended: provide extended option). - This is because this option will be required in the next version and not be optional anymore. For more info on the extended option, please refer to the readme of body-parser.