Node.js Express。bodyParser 的大主体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25332561/
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
Node.js Express. Large body for bodyParser
提问by user2856066
I use Express.js ver 4.2 and want to parse a large post (150K - 1M) but receives the error message "request entity too large". It seems that the limit is 100 K. I don't now how to change the limit in Express 4. In Express 3.x I just did -
我使用 Express.js 4.2 版并想解析一个大帖子 (150K - 1M),但收到错误消息“请求实体太大”。似乎限制是 100 K。我现在不知道如何更改 Express 4 中的限制。在 Express 3.x 中我刚刚做了 -
app.use(express.json({limit: '5mb'}));
app.use(express.urlencoded({limit: '5mb'}));
How can I change the limit in Express 4 ?
如何更改 Express 4 中的限制?
Thanks for any help.
谢谢你的帮助。
回答by mscdex
With Express 4 you have to install the body-parsermodule and use that instead:
使用 Express 4,您必须安装body-parser模块并使用它:
var bodyParser = require('body-parser');
// ...
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({limit: '5mb'}));
回答by blackmiaool
Mscdex's code works, but we should add another parameter to avoid warning now.
Mscdex 的代码有效,但我们现在应该添加另一个参数以避免警告。
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
回答by Ryan
Express v4.x.x
快递 v4.xx
Node.js v9.x.x
Node.js v9.xx
This is dependent on whether you are receiving data as JSON or via parameterized URL query.
这取决于您是以 JSON 形式接收数据还是通过参数化 URL 查询接收数据。
I had the same problem sending a large JSON buffer. The file was ~ 43KB and I was incoming from my middleware to an express API.
我在发送大型 JSON 缓冲区时遇到了同样的问题。该文件大约为 43KB,我从中间件传入一个快速 API。
I handled it as such:
我是这样处理的:
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: false }));
This corrected the issue for me, when the body was a large JSON object.
当主体是一个大型 JSON 对象时,这为我纠正了这个问题。

