NodeJS路由器有效负载过大 [英] NodeJS Router payload too large

查看:110
本文介绍了NodeJS路由器有效负载过大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在nodejs应用程序中创建其余端点,如下所示:

I'm creating rest endpoints in my nodejs application as follows:

在我的server.js中,我有以下代码:

In my server.js I have the following code:

var express = require('express');
var app = express();
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
app.use(require('./routes/APIRoutes'));

我也尝试了下面的代码,而不是按照潜在重复问题的建议使用express.json和expres.urlencoded.

I also tried the code below instead of using the express.json and expres.urlencoded as suggested on the potential duplicate question.

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(require('./routes/APIRoutes'));

在我的APIRoutes文件中,我有以下代码:

In my APIRoutes file I have the following code:

/* --- INIT --- */
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.json({ limit: '50mb' }));
router.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

var urlencodedParser = bodyParser.urlencoded({ limit: '50mb', extended: true });

router.post('...', urlencodedParser, function (req, res) {
...
});

我尝试了不同的组合和顺序以将限制设置得更高.但是目前,每次我发送2kb的有效负载时,都会返回413错误(有效负载过大").

I tried different combinations and orders for setting the limit higher. But currently every time I send a payload of 2kb, I get an 413 "(Payload Too Large)" error back.

任何帮助设置极限的正确位置是什么?

Any help what the correct location is for setting the limit?

推荐答案

我在建议的TKJohn副本的一个不可接受的答案中找到了解决问题的方法. 我必须添加参数限制,以便我的代码变为:

I found the solution for my problem in one of the unaccepted answers of the suggested duplicate of TKJohn. I had to add the parameter limit so that my code became:

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));

添加后它起作用了!

我也很想从APIRoutes.js删除代码

I was also ablo to remove the code from APIRoutes.js

router.use(bodyParser.json({ limit: '50mb' }));
router.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

这篇关于NodeJS路由器有效负载过大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆