Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

body parser - Node.js Express.js bodyParser POST limit

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...