错误:请求实体太大


本文翻译自:Error: request entity too large

I'm receiving the following error with express: 我收到以下错误快递:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Error: request entity too large
    at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/node_modules/raw-body/index.js:16:15)
    at json (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/json.js:60:5)
    at Object.bodyParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.cookieParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.logger (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/logger.js:158:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.staticMiddleware [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
TypeError: /Users/michaeljames/Documents/Projects/Proj/mean/app/views/includes/foot.jade:31
    29| script(type="text/javascript", src="/js/socketio/connect.js")
    30|
  > 31| if (req.host='localhost')
    32|     //Livereload script rendered
    33|     script(type='text/javascript', src='http://localhost:35729/livereload.js')  
    34|

Cannot set property 'host' of undefined
    at eval (eval at <anonymous> (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:152:8), <anonymous>:273:15)
    at /Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:153:35
    at Object.exports.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:197:10)
    at Object.exports.renderFile (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:233:18)
    at View.exports.renderFile [as engine] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:218:21)
    at View.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/application.js:504:10)
    at ServerResponse.res.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/response.js:801:7)
    at Object.handle (/Users/michaeljames/Documents/Projects/Proj/mean/config/express.js:82:29)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:188:17)

POST /api/0.1/people 500 618ms

I am using meanstack. 我正在使用meanstack。 I have the following use statements in my express.js 我在express.js中有以下用法语句

1
2
//Set Request Size Limit
app.use(express.limit(100000000));

Within fiddler I can see the content-length header with a value of: 1078702 在fiddler中,我可以看到内容长度标题,其值为:1078702

I believe this is in octets, this is 1.0787 megabytes. 我相信这是八位字节,这是1.0787兆字节。

I have no idea why express is not letting me post the json array I was posting previously in another express project that was not using the mean stack project structure. 我不知道为什么express不让我发布我之前在另一个没有使用平均堆栈项目结构的快速项目中发布的json数组。


#1楼

参考:https://stackoom.com/question/1LZQP/错误-请求实体太大


#2楼

I don't think this is the express global size limit, but specifically the connect.json middleware limit . 我认为这不是明确的全局大小限制,而是具体的connect.json中间件限制 。 This is 100kb by default when you use express.bodyParser() and don't provide a limit option. 默认情况下,当您使用express.bodyParser()并且不提供limit选项时,这是100kb。

Try: 尝试:

1
app.post('/api/0.1/people', express.bodyParser({limit: '5mb'}), yourHandler);


#3楼

I had the same error recently, and all the solutions I've found did not work. 我最近遇到了同样的错误,我找到的所有解决方案都没有用。

After some digging, I found that setting app.use(express.bodyParser({limit: '50mb'})); 经过一番挖掘,我发现设置app.use(express.bodyParser({limit: '50mb'})); did set the limit correctly. 确实正确设置了限制。

When adding a console.log('Limit file size: '+limit); 添加console.log('Limit file size: '+limit); in node_modules/express/node_modules/connect/lib/middleware/json.js:46 and restarting node, I get this output in the console: node_modules/express/node_modules/connect/lib/middleware/json.js:46并重新启动节点,我在控制台中获得此输出:

1
2
3
4
5
6
Limit file size: 1048576
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Limit file size: 52428800
Express server listening on port 3002

We can see that at first, when loading the connect module, the limit is set to 1mb (1048576 bytes). 我们可以看到,首先,加载connect模块时,限制设置为1mb(1048576字节)。 Then when I set the limit, the console.log is called again and this time the limit is 52428800 (50mb). 然后,当我设置限制时,再次调用console.log ,这次限制为52428800(50mb)。 However, I still get a 413 Request entity too large . 但是,我仍然得到一个413 Request entity too large

Then I added console.log('Limit file size: '+limit); 然后我添加了console.log('Limit file size: '+limit); in node_modules/express/node_modules/connect/node_modules/raw-body/index.js:10 and saw another line in the console when calling the route with a big request (before the error output) : node_modules/express/node_modules/connect/node_modules/raw-body/index.js:10中,当调用具有大请求的路由时(在错误输出之前),在控制台中看到另一行:

1
Limit file size: 1048576

This means that somehow, somewhere, connect resets the limit parameter and ignores what we specified. 这意味着以某种方式,某处, connect重置limit参数并忽略我们指定的内容。 I tried specifying the bodyParser parameters in the route definition individually, but no luck either. 我尝试单独在路由定义中指定bodyParser参数,但也没有运气。

While I did not find any proper way to set it permanently, you can " patch " it in the module directly. 虽然我没有找到任何正确的永久设置方法,但您可以直接在模块中“ 修补 ”它。 If you are using Express 3.4.4, add this at line 46 of node_modules/express/node_modules/connect/lib/middleware/json.js : 如果您使用的是Express 3.4.4,请在node_modules/express/node_modules/connect/lib/middleware/json.js第46行添加:

1
limit = 52428800; // for 50mb, this corresponds to the size in bytes

The line number might differ if you don't run the same version of Express. 如果您不运行相同版本的Express,则行号可能会有所不同。 Please note that this is bad practice and it will be overwritten if you update your module. 请注意,这是不好的做法 ,如果您更新模块,它将被覆盖

So this temporary solution works for now, but as soon as a solution is found (or the module fixed, in case it's a module problem) you should update your code accordingly. 所以这个临时解决方案现在可以工作,但是一旦找到解决方案(或者模块已修复,以防模块出现问题),您应该相应地更新代码。

I have opened an issue on their GitHub about this problem. 我在他们的GitHub上打开了一个关于这个问题的问题。

[edit - found the solution] [编辑 - 找到解决方案]

After some research and testing, I found that when debugging, I added app.use(express.bodyParser({limit: '50mb'})); 经过一些研究和测试,我发现在调试时,我添加了app.use(express.bodyParser({limit: '50mb'})); , but after app.use(express.json()); ,但 app.use(express.json()); 之后 app.use(express.json()); . Express would then set the global limit to 1mb because the first parser he encountered when running the script was express.json() . 然后,Express会将全局限制设置为1mb,因为运行脚本时遇到的第一个解析器是express.json() Moving bodyParser above it did the trick. 移动bodyParser就可以了。

That said, the bodyParser() method will be deprecated in Connect 3.0 and should not be used. 也就是说, bodyParser()方法将在Connect 3.0中弃用,不应该使用。 Instead, you should declare your parsers explicitly, like so : 相反,您应该明确声明您的解析器,如下所示:

1
2
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));

In case you need multipart (for file uploads) see this post . 如果您需要multipart(用于文件上传),请参阅此帖子 。

Second edit 第二次编辑

Note that in Express 4, instead of express.json() and express.urlencoded() , you must require the body-parser module and use its json() and urlencoded() methods, like so : 请注意,在Express 4中,而不是express.json()express.urlencoded() ,您必须要求body-parser模块并使用其json()urlencoded()方法,如下所示:

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

If the extended option is not explicitly defined for bodyParser.urlencoded() , it will throw a warning ( body-parser deprecated undefined extended: provide extended option ). 如果没有为bodyParser.urlencoded()显式定义extended选项,它将抛出一个警告( body-parser deprecated undefined extended: provide extended option )。 This is because this option will be required in the next version and will not be optional anymore. 这是因为在下一个版本中将需要此选项,并且不再是可选的。 For more info on the extended option, please refer to the readme of body-parser . 有关extended选项的更多信息,请参阅body-parser的自述文件 。


#4楼

I've used another practice for this problem with multer dependancie. 我用multer dependancie来解决这个问题。

Example: 例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
multer = require('multer');

var uploading = multer({
  limits: {fileSize: 1000000, files:1},
});

exports.uploadpictureone = function(req, res) {
  cloudinary.uploader.upload(req.body.url, function(result) {
    res.send(result);
  });
};

module.exports = function(app) {
    app.route('/api/upload', uploading).all(uploadPolicy.isAllowed)
        .post(upload.uploadpictureone);
};

#5楼

I use Express 4. 我使用Express 4。

In my case it was not enough to add these lines : 在我的情况下,添加这些行是不够的:

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

I tried adding the parameterLimit option on urlencoded function as the documentation says and error no longer appears. 我尝试在urlencoded函数上添加parameterLimit选项,因为文档说并且错误不再出现。

The parameterLimit option controls the maximum number of parameters that are allowed in the URL-encoded data. parameterLimit选项控制URL编码数据中允许的最大参数数。 If a request contains more parameters than this value, a 413 will be returned to the client. 如果请求包含的参数多于此值,则会将413返回给客户端。 Defaults to 1000. 默认为1000。

Try with this code: 试试这段代码:

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

#6楼

2016, none of the above worked for me until i explicity set the 'type' in addition to the 'limit' for bodyparser, example: 2016年,除了bodyparser的'limit'之外,除了明确设置'type'之外,以上都没有为我工作,例如:

1
2
3
4
5
6
  var app = express();
  var jsonParser       = bodyParser.json({limit:1024*1024*20, type:'application/json'});
  var urlencodedParser = bodyParser.urlencoded({ extended:true,limit:1024*1024*20,type:'application/x-www-form-urlencoding' })

  app.use(jsonParser);
  app.use(urlencodedParser);