节点js请求实体太大而multer上传 [英] Node js request entity too large with multer upload

查看:64
本文介绍了节点js请求实体太大而multer上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经尝试了不同的方式上传200k文件增加了限制更改参数,做了我改变了multer的一切。 Fucei一切我知道我在堆栈中读到的东西,我在google上发现基本的谷歌搜索完成了我的问题并没有起来但是下来的图片就像一个魅力。服务器支持上传图像我atualemente用php完成这项任务,但如果没有在nodejs中滚动,我将在php中执行相同的文件。感谢收听。



我的代码

  var express = require( '表达'); 
var app = express();
var http = require('http')。服务器(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();
},
限制:{
fieldNameSize:200,
文件:5,
字段: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){
如果(错误){
返回console.log(错误);
}

/ *如果数据* /
fs.unlinkSync(文件)做某事.path);
});

});

http.listen(8080,function(){
console.log(Conectado e escutando na porta:8080);


} );

错误显示给我:错误:请求实体太大

 在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)
在urlencodedParser(/var/www/likeyounode/node_modules/body-parser/lib/types/urlencoded.js:109:5)
在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
在Function.process_params(/ var / www / likeyounode / node_modules / express / lib / router / index.js:330:12)
at next(/ var / www / likeyou node / node_modules / express / lib / router / index.js:271:10)
at expressInit(/var/www/likeyounode/node_modules/express/lib/middleware/init.js:33:5)

我查看了我的所有代码,只需要提供大量证据来证明我的需求超过了揭示了我项目的大部分内容。



我所做的身体中有一些配置,如果你注意到它们在内容中有说明,那么怀疑就不具备所有功能你运行自己的应用程序。

解决方案

在我的情况下,设置限制后@ node.js (webapp)

var express = require('express'); var app = express();
var bodyParser = require('body-parser'); app.use(bodyParser.json({极限: 50MB})); app.use(bodyParser.urlencoded({extended:true,limit:'50mb'}));



我还(最重要的)必须设置限制@ nginx (webapp反向代理)

#最大上传大小。
client_max_body_size 50M;


well I've tried different ways to upload a 200k file increased the limit changed parameters, did everything I changed the multer. Fucei everything I knew what I read in the stack, which I found on google the basic google search is done my problem and not up but down the pictures works like a charm. the server supports upload images I atualemente do this task with php but if not roll in nodejs, I'll do the same files in php. thanks for listening.

My code

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


});

The error show to me it's it: Error: request entity too large

        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)

I reviewed all my code and it is only necessary that amount of evidence to show what I want more than that will reveal much of my project.

There are some configurations in the body I did and if you notice they are illustrated in the content, being a doubt will not have all the functions you away running its own application.

解决方案

In my case, after setting the limit @ 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'}));

I also (most important) had to set up the limit @ nginx (webapp reverse proxy) # Max upload size. client_max_body_size 50M;

这篇关于节点js请求实体太大而multer上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆