简体   繁体   English

NodeJS 路由器负载太大

[英]NodeJS Router payload too large

I'm creating rest endpoints in my nodejs application as follows:我在我的 nodejs 应用程序中创建 rest 端点,如下所示:

In my server.js I have the following code:在我的 server.js 中,我有以下代码:

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'));

I also tried the code below instead of using the express.json and expres.urlencoded as suggested on the potential duplicate question.我还尝试了下面的代码,而不是按照潜在重复问题的建议使用 express.json 和 expres.urlencoded。

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'));

In my APIRoutes file I have the following code:在我的 APIRoutes 文件中,我有以下代码:

/* --- 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) {
...
});

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.但目前每次我发送 2kb 的有效载荷时,我都会收到 413“(有效载荷太大)”错误。

Any help what the correct location is for setting the limit?任何帮助设置限制的正确位置是什么?

I found the solution for my problem in one of the unaccepted answers of the suggested duplicate of TKJohn.我在建议的 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 }));

After the addition it worked!添加后有效!

I was also ablo to remove the code from APIRoutes.js我还可以从 APIRoutes.js 中删除代码

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

You can use this straightaway in express if you are using the latest version of express .如果您使用的是最新版本的express ,则可以直接在express中使用它。

    let express = require('express');

    let app = express();

    app.use(express.json({limit: '50mb', extended: true}));
    app.use(express.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));

Please note that this will only work with express versions bundled with bodyParser ( bodyParser was added back to express in release 4.16.0 )请注意,这仅适用于与bodyParser捆绑在一起的express版本( bodyParser4.16.0版本中被添加回express

This worked for me这对我有用

this.express.use(bodyParser.json({ limit: '50mb' }));
this.express.use(bodyParser.urlencoded({ limit: "50mb" ,extended: true, parameterLimit: 50000 }));

Tried looking online for past couple of hours and found no solution to 413 Payload尝试在线查找过去几个小时,但没有找到 413 Payload 的解决方案

Turns out in urlencoded.js within the body-parser module there are additional options which take care of limits.结果在 body-parser 模块中的urlencoded.js中有额外的选项来处理限制。

Options like parameterLimit and arrayLimit are items to amend if your parameter or array size exceeds the limit that is granted by default (which is 100 for array size).如果您的参数或数组大小超过默认授予的限制(数组大小为 100),则parameterLimitarrayLimit等选项是要修改的项目。

I was having the same error.我有同样的错误。 I have tried all those suggestions or solutions but failed to work.我已经尝试了所有这些建议或解决方案,但都没有奏效。

Are you uploading file?你在上传文件吗?

In my case, I was using multer and also was uploading files with it.就我而言,我使用的是multer并且还使用它上传文件。

The simple way to solve this is to not set a Content-Type.解决这个问题的简单方法是不设置 Content-Type。

my problem was that I was setting on my headers: Content-Type: application/json and I am [was] using multer: so my app was trying to parse the uploaded audio file to JSON when the solution:我的问题是我在标题上设置: Content-Type: application/json并且我 [was] 使用 multer: 所以我的应用程序试图在解决方案时将上传的音频文件解析为 JSON:

...
app.use(express.json({limit: '50mb', extended: true}));
app.use(express.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
...

...is used and or the server complaints of large payload when it is removed. ...在删除时使用和/或服务器抱怨大负载。

Currently, my code is working目前,我的代码正在运行

on my code, I have just this with the above solution that was failing base on my app case (file upload):在我的代码中,基于我的应用案例(文件上传),我的上述解决方案失败了:

const express = require('express');

...

const app = express();

app.use(express.json());
...

...And it is working because I removed Content-Type on my postman headers. ...它正在工作,因为我删除了邮递员标题上的 Content-Type。

Make sure you are not using Content-Type on the headers when sending the request.确保在发送请求时没有在标头上使用 Content-Type。 If you care to know I was not setting it either on the express app(API).如果您想知道我没有在 Express 应用程序 (API) 上设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM