Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the bodyParser upload limit to specific routes rather than globally in the middleware?

Here's an example (Express 3) middleware setup thats worked for me globally:

app.configure(function () {
    app.use(express.static(__dirname + "/public"));
    app.use(express.bodyParser({
          keepExtensions: true,
          limit: 10000000, // set 10MB limit
          defer: true              
    }));
    //... more config stuff
}

For security reasons, I don't want to allow 500GB+ posts on routes other than /upload, so I'm trying to figure out how to specify the limit on specific routes, rather than globally in the middleware.

I know the multipart middleware in bodyParser() already sniffs out content types, but I want to limit it even further.

This does not seem to work in express 3:

app.use('/', express.bodyParser({
  keepExtensions: true,
  limit: 1024 * 1024 * 10,
  defer: true              
}));
app.use('/upload', express.bodyParser({
  keepExtensions: true,
  limit: 1024 * 1024 * 1024 * 500,
  defer: true              
}));

I get an error Error: Request Entity Too Large when I try to upload a 3MB file on the upload URL.

How do you do this correctly?

like image 322
qodeninja Avatar asked Sep 23 '13 01:09

qodeninja


People also ask

What is bodyParser limit?

limit. Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the bytes library for parsing. Defaults to '100kb' .

What should I use instead of bodyParser?

bodyParser depends on multipart , which behind the scenes uses multiparty to parse uploads. You can use this module directly to handle the request. In this case you can look at multiparty's API and do the right thing. There are also alternatives such as busboy, parted, and formidable.

Is bodyParser deprecated 2021?

body parser package is deprecated. If you are using latest version of express you don't have to install body-parser package.

What is the benefit of using extended false in bodyParser?

The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false ) or the qs library (when true ). The “extended” syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded.


2 Answers

Actually as suggested by hexacyanide above, app.use() works on limiting single route.
The problem comes from the ordering of route paths.
Going to example above, if you put '/upload' first, then the bodyParser should match that rule first.

So put the code like so (I am using Express 4.0 +):

app.use("/PATH_WITH_LIMIT", bodyParser({ 
    limit: 1024 * 1000
}));
app.use("/",bodyParser());

You can see how express binds the middleware on app.use() method call here.

like image 96
Plyto Avatar answered Oct 19 '22 22:10

Plyto


Just specify the optional path option when using app.use().

app.use('/', express.bodyParser({
  keepExtensions: true,
  limit: 1024 * 1024 * 10,
  defer: true              
}));
app.use('/upload', express.bodyParser({
  keepExtensions: true,
  limit: 1024 * 1024 * 1024 * 500,
  defer: true              
}));
like image 29
hexacyanide Avatar answered Oct 20 '22 00:10

hexacyanide