How to limit upload file size in express.js

Express uses connect middleware, you can specify the file upload size by using the following

app.use(express.limit('4M'));

Connect Limit middleware


// Comment sart   
// app.use(express.bodyParser({
//    uploadDir:'./Temp',
//    maxFieldsSize:'2 * 1024 * 1024 * 1024 ',
// }));  

// Add this code for maximun 150mb 
app.use(bodyParser.json({limit: '150mb'}));
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
limit: '150mb',
extended: true
})); 

// I did it Okay. Goood luck 

In 2020 Express uses the body-parser urlencoded function to control the limit. http://expressjs.com/en/4x/api.html#express.urlencoded

These are the default settings inside node_modules>body-parser>lib>types>urlencoded.js https://www.npmjs.com/package/body-parser

  var extended = opts.extended !== false
  var inflate = opts.inflate !== false
  var limit = typeof opts.limit !== 'number'
    ? bytes.parse(opts.limit || '100kb')
    : opts.limit
  var type = opts.type || 'application/x-www-form-urlencoded'
  var verify = opts.verify || false

You can see here that the default setting for limit is 100kb. so in order to up that you can use

app.use(express.urlencoded({ extended: false, limit: '2gb' }));

here are the filetype options available via NPM package bytes ( used by bodyparser ) https://www.npmjs.com/package/bytes

"b" for bytes
"kb" for kilobytes
"mb" for megabytes
"gb" for gigabytes
"tb" for terabytes
"pb" for petabytes

I'm sure this was overkill but I hope this helps the next person.