'Express Body-parser File upload size limit not working

My express app:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var less = require("express-less")

var session = require("express-session")
var FileStore = require("session-file-store")(session)

var router = require('./router');

var app = express();

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

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use('/css', less(__dirname + '/less'));

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({
  secret: "secret message that is obviously not the one i'm using in my code",
    resave: true,
    saveUninitialized: true
}));

app.use('/', router);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

(It's just the code generated by express-generator, but with a few additions)

The lines

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

are supposed to set the maximum limit to what ever it is that I set it to, however, I've searched the internet far and wide and have yet to come across a solution that works. I get a 413 (payload too large) error when I attemt to send an image greater than 100kb via XMLHttpRequest to the server.

It was also recommended to me that I use 'MB' instead of 'mb' but that didn't work.



Solution 1:[1]

The problem in my case, as in @ch1pn3ss's case, was that there was a proxy sitting between my app and the client/user. In @ch1pn3ss's case it was nginx, and in my case it was a node/express-based reverse proxy server. So if there's something between your server and the user, then you'll have to raise limits on that too.

Another mistake that I've made before is having multiple instances of the parser use call, like:

app.use(bodyParser.json({limit: '1mb'}));

// ... later in the code:
app.use(bodyParser.json({limit: '50mb'}));

and I've been changing the latter one and not realising that it's actually being ignored because the former one has set the limit to 1mb.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joe