Node.js Express.js bodyParser POST limit
Asked Answered
I

2

16

I'm trying to set the limit option for bodyParser.urlencodedParser as my POST data is bigger than the default value. My code currently looks like the following but whatever i try i always receive the following error:

Error: request entity too large

var express = require('express');
var router = express.Router();
var jsonfile = require('jsonfile');
var bodyParser = require('body-parser');

var urlencodedParser = bodyParser.urlencoded({limit: '5mb'});

router.post('/data', urlencodedParser ,function(req, res) {


    if(typeof req.body.data === 'undefined')
    {
       console.log('Missing data');
       res.status(500).send({ error: 'Missing Data Parameters' });
       return;
    }

    // Static return value
    var output = [ 
        {"f" : "1"},
        {"f" : "2"},
        {"f" : "3"}
    ];

    res.send(output);
}

Any help greatly appreciated.

Instantly answered 12/8, 2015 at 13:47 Comment(2)
did you try making the limit bigger?Aracelis
yes i've tried 50mb but still get the errorInstantly
E
30

I also experienced same error with this (using MEAN.js), have tried to put the limit on the express configuration (as answered in others thread) but still with no luck with that.

I figured out that sometimes the problem is not only the limit, if you've passed lot of parameter you need to set the parameter limit as well.

I've successfully fixed the problem by also set the parameter limit as below:

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

 app.use(bodyParser.urlencoded({
    parameterLimit: 100000,
    limit: '50mb',
    extended: true
  }));

the default value for parameter limit is 1000.

Source from body-parser: https://github.com/expressjs/body-parser#parameterlimit

Eichmann answered 30/1, 2017 at 6:28 Comment(0)
F
21

I was having trouble sending image data over ajax and getting 'Error: request entity too large', managed to solve it by adding

app.use(bodyParser.json({limit: '50mb', type: 'application/json'}));

but the most important part was to make sure that i called

app.use(bodyParser());

after i set the limit, hope this helps!!

Frustule answered 25/7, 2016 at 0:41 Comment(2)
Now in 2019, this generates an error: deprecated body-parser: use individual json/urlencoded middlewaresMirador
@Mirador That's a warning instead of an error. Typically, deprecations will only throw a warning, not an error so updates will still support legacy implementations. It should still work, but if you can implement the new functionality, you should.Sure

© 2022 - 2024 — McMap. All rights reserved.