我的代码
var express = require('express');
var app = express();
var http = require('http').Server(app);
var bodyParser = require("body-parser");
var fs = require('fs');
var multer = require('multer');
var destino = '/var/www/upload/';
var upload = multer({dest: 'uploads/',
rename: function (fieldname, filename) {
return filename + Date.now();
},
limits: {
fieldNameSize: 200,
files: 5,
fields: 5
}
});
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.post('/image', upload.single('message'), function (req, res) {
var file = req.file;
fs = require('fs')
fs.readFile(file.path, 'base64', function (err, data) {
if (err) {
return console.log(err);
}
/* do something if data*/
fs.unlinkSync(file.path);
});
});
http.listen(8080, function () {
console.log("Conectado e escutando na porta: 8080");
});
错误告诉我它:错误:请求实体太大
at readStream (/var/www/likeyounode/node_modules/body-parser/node_modules/raw-body/index.js:196:17)
at getRawBody (/var/www/likeyounode/node_modules/body-parser/node_modules/raw-body/index.js:106:12)
at read (/var/www/likeyounode/node_modules/body-parser/lib/read.js:76:3)
at urlencodedParser (/var/www/likeyounode/node_modules/body-parser/lib/types/urlencoded.js:109:5)
at Layer.handle [as handle_request] (/var/www/likeyounode/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/var/www/likeyounode/node_modules/express/lib/router/index.js:312:13)
at /var/www/likeyounode/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/var/www/likeyounode/node_modules/express/lib/router/index.js:330:12)
at next (/var/www/likeyounode/node_modules/express/lib/router/index.js:271:10)
at expressInit (/var/www/likeyounode/node_modules/express/lib/middleware/init.js:33:5)
我查看了我的所有代码,只需要提供大量证据来展示我想要的东西,这将大大揭示我的项目。
在我所做的身体中有一些配置,如果你注意到它们在内容中有说明,那么怀疑你将无法运行自己运行的所有功能。
答案 0 :(得分:18)
在我的情况下,在设置限制@ node.js (webapp)之后
var express = require('express'); var app = express();
var bodyParser = require('body-parser'); app.use(bodyParser.json({limit:'50mb'})); app.use(bodyParser.urlencoded({extended:true, limit:'50mb'}));
我(最重要的)必须设置限制@ nginx (webapp反向代理)
# Max upload size.
client_max_body_size 50M;
答案 1 :(得分:2)
我无法通过您的所有代码告诉您,但只需查看错误,您就可以使用以下内容设置正文帖子大小:
https://www.npmjs.com/package/multer#limits
multer({
storage: storage,
limits: { fileSize: maxSize }
})
如果这不能解决您的问题,bodyParser也有限制。 可以找到它的选项:
https://github.com/expressjs/body-parser#limit
bodyParser({limit: '4MB'});