Node.js Express.js bodyParser POST 限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31967138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:14:24  来源:igfitidea点击:

Node.js Express.js bodyParser POST limit

node.jsbody-parser

提问by scgy

I'm trying to set the limitoption for bodyParser.urlencodedParseras 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:

我正在尝试设置limit选项,bodyParser.urlencodedParser因为我的 POST 数据大于默认值。我的代码目前如下所示,但无论我尝试什么,我总是收到以下错误:

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.

非常感谢任何帮助。

回答by IMDWPD

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.

我也遇到了同样的错误(使用 MEAN.js),试图限制快速配置(如其他线程中的回答),但仍然没有运气。

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.

参数限制的默认值为 1000。

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

来自 body-parser 的来源:https: //github.com/expressjs/body-parser#parameterlimit

回答by Matt

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

我在通过 ajax 发送图像数据时遇到问题并收到“错误:请求实体太大”,设法通过添加来解决它

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!!

在我设置限制后,希望这会有所帮助!!