本文翻译自: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
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
When adding a
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
Then I added
1 | Limit file size: 1048576 |
This means that somehow, somewhere,
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
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
That said, the
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
1 2 3 | var bodyParser = require('body-parser'); app.use(bodyParser.json({limit: '50mb'})); app.use(bodyParser.urlencoded({limit: '50mb', extended: true})); |
If the
#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); |