43


I use Express.js ver 4.2 and want to parse a large post (150K - 1M) but receives the error message "request entity too large". It seems that the limit is 100 K. I don't now how to change the limit in Express 4. In Express 3.x I just did -

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

How can I change the limit in Express 4 ?

Thanks for any help.

2
  • 1
    The limit isn't in Express, but in the middleware. What middleware are you using in Express 4?
    – Brad
    Aug 15, 2014 at 19:07
  • If anyone has been pulling their hair out (like I was) because body-parser doesn't seem to be listening to your limits, check out this answer: stackoverflow.com/a/40745569/993683 ==> could be nginx (or equivalent) limit
    – user993683
    Jun 2, 2017 at 16:41

3 Answers 3

76

With Express 4 you have to install the body-parser module and use that instead:

var bodyParser = require('body-parser');

// ...

app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({limit: '5mb'}));
9
  • 4
    Hi. You got me on the right track. I have already done it but with the big mistake that I did it in two step. First only app.use(bodyParser.json()); and the agin app.use(bodyParser.json({limit: '5mb'})); After your answer I double checked my code and removed the first app.use and it all worked. Stupid me and thanks a lot for you quick answer and help to find my mistake. Thanks. Aug 15, 2014 at 19:35
  • So, if someone sends along a profile picture in a form POST, does that count toward the limit? Or is that handled by different middleware anyway, so it doesn't? May 15, 2015 at 17:02
  • 6
    @Costa the bodyParser module does not handle multipart/form-data requests (which would be the case if you are talking about uploading files).
    – mscdex
    May 15, 2015 at 17:14
  • 1
    @mscdex Unless he is sending a base64 encoded image.
    – Diosney
    Aug 31, 2015 at 15:07
  • 2
    For those new to the router system in Express: These changes go on the main app.js page above your routes, not within the routes/.js files.
    – PeterM
    May 5, 2016 at 22:15
11

Mscdex's code works, but we should add another parameter to avoid warning now.

app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
0
1

Express v4.x.x

Node.js v9.x.x

This is dependent on whether you are receiving data as JSON or via parameterized URL query.

I had the same problem sending a large JSON buffer. The file was ~ 43KB and I was incoming from my middleware to an express API.

I handled it as such:

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

This corrected the issue for me, when the body was a large JSON object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.