Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js request entity too large with multer upload

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.

like image 230
Gustavo Castro Avatar asked Apr 07 '16 19:04

Gustavo Castro


People also ask

What is diskStorage in multer?

DiskStorage. The disk storage engine gives you full control on storing files to disk. const storage = multer. diskStorage({ destination: function (req, file, cb) { cb(null, '/tmp/my-uploads') }, filename: function (req, file, cb) { const uniqueSuffix = Date.

Can I use multer without Express?

You cannot use Multer without because it's Express middleware.

How does Nodejs store multer files?

Create a folder- images, in the root directory. const imageStorage = multer. diskStorage({ // Destination to store image destination: 'images', filename: (req, file, cb) => { cb(null, file. fieldname + '_' + Date.


2 Answers

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;
like image 142
EMX Avatar answered Sep 23 '22 08:09

EMX


I can't tell by all of your code but just by looking at the error, you should be able to set the body post size with the following:

https://www.npmjs.com/package/multer#limits

 multer({
    storage: storage,
    limits: { fileSize: maxSize }
 })

If that doesn't solve your issue, bodyParser also has a limit. It's options can be found:

https://github.com/expressjs/body-parser#limit

bodyParser({limit: '4MB'});
like image 36
Eric Avatar answered Sep 23 '22 08:09

Eric