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.

user2856066
  • 781
  • 1
  • 8
  • 18
  • 1
    The limit isn't in Express, but in the middleware. What middleware are you using in Express 4? – Brad Aug 15 '14 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 `limit`s, check out this answer: https://stackoverflow.com/a/40745569/993683 ==> could be `nginx` (or equivalent) limit –  Jun 02 '17 at 16:41

3 Answers3

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'}));
mscdex
  • 93,083
  • 13
  • 170
  • 135
  • 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. – user2856066 Aug 15 '14 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? – Costa May 15 '15 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 '15 at 17:14
  • 1
    @mscdex Unless he is sending a base64 encoded image. – Diosney Aug 31 '15 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. – Peter Mark May 05 '16 at 22:15
  • Hi, I am also facing the "request entity too large " issue with Oracle MCS. But i am not using ExpressJS – Arj 1411 Nov 10 '16 at 05:43
  • what does "app" refers? – Arj 1411 Nov 10 '16 at 12:52
  • bodyParser.urlencoded is deprecated. what is it for? – newman Feb 27 '17 at 03:52
  • doesnt work: SyntaxError: Unexpected token # in JSON at position 0 at JSON.parse () at createStrictSyntaxError (C:\node\webchat\server\node_modules\body-parser\ lib\types\json.js:157:10) – nikksan Mar 03 '18 at 16:16
11

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

app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
blackmiaool
  • 4,964
  • 2
  • 17
  • 36
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.

wattry
  • 720
  • 7
  • 17