gotorres.gt18
gotorres.gt18

Reputation: 13

Node Express - PayloadTooLargeError: request entity too large

I got the following error when I try to upload a file(.docx, .xlsx, etc.) higher than 1M

PayloadTooLargeError: request entity too large
at readStream (C:\Users\GoTor\Documents\Node\BackEnd\node_modules\raw-body\index.js:155:17)
at getRawBody (C:\Users\GoTor\Documents\Node\BackEnd\node_modules\raw-body\index.js:108:12)
at read (C:\Users\GoTor\Documents\Node\BackEnd\node_modules\body-parser\lib\read.js:77:3)
    ...

the code is following

...

var mongoose = require('mongoose')
var express = require("express");
var app = require('./app');

app.use(bodyParser.json({ limit: "50mb" }))
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 }))

//3.
var user_mng = 'user';
var pass_mng = 'pass';

//server
var port = process.env.PORT || 9090;
mongoose.Promise = global.Promise;
mongoose.connect(`mongodb://hostname:0101/dbname`, 
    {
        auth: { "authSource": "user" },
        user: user_mng,
        pass: pass_mng,
        useMongoClient: true})
    .then(()=>{
        const server = app.listen(port, () => {
            const host_srv = server.address().address;
            const port_srv = server.address().port;
        });
    })
    .catch(err=> console.log(err));

package.json:

"dependencies": {
...
"body-parser": "^1.19.0",
"express": "^4.17.1",
...
}

So I tried some solutions, but these didn't work for me.

this:

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

this

app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));

And this:

app.use(bodyParser.json({limit:1024*1024*20, type:'application/json'}));
app.use(bodyParser.urlencoded({extended:true,limit:1024*1024*20,type:'application/x-www-form-urlencoding' }));

Upvotes: 1

Views: 9573

Answers (2)

Smurtiranjan Sahoo
Smurtiranjan Sahoo

Reputation: 44

The problem is not with node or express it's with the nginx. I'm using aws ebs to manage my application. to solve the issue you just need to make a file in /.platform/nginx/conf.d/proxy.conf with the content

client_max_body_size 100M;

Upvotes: 0

Shariful Islam Mubin
Shariful Islam Mubin

Reputation: 2286

Replace your 2 lines of app.use code to this:

app.use(express.json({limit: '25mb'}));
app.use(express.urlencoded({limit: '25mb', extended: true}));

Upvotes: 3

Related Questions