BJPrim
BJPrim

Reputation: 358

Express: limit request size

To limit the request size to 123 bytes, I use the following which works fine:

app.use(bodyParser.urlencoded({
    extended: true,
    limit: 123
}));

To parse all requests except for "/specialRequest", I use the following which works fine as well:

app.use(/^(?!\/specialRequest)/,bodyParser.urlencoded({
    extended: true
}));

But I fail to limit the request size of all requests to 123 and parse all requests except for "/specialRequest". Here is my current code:

app.use(/^(?!\/specialRequest)/,bodyParser.urlencoded({
    extended: true,
    limit: 123
}));

However, this only limits the requests size for requests different than "/specialRequest". How can I limit the request size of all requests to 123 and parse all requests except for "/specialRequest"?

Upvotes: 1

Views: 3382

Answers (1)

robertklep
robertklep

Reputation: 203241

If you want the request body for /specialRequest to be limited in size but not parsed, you can use bodyParser.raw(). In that case, req.body will be a Buffer instance that contains the request body as-is (unparsed, although it will be inflated if it was presented as gzipped or deflated data; this behaviour can be disabled through its options).

You need to declare it before you insert the bodyParser.urlencoded() middleware:

app.post('/specialRequest', bodyParser.raw({ limit : 123, type : '*/*' }), function(req, res) {
  ...
});

app.use(bodyParser.urlencoded({
    extended: true,
    limit: 123
}));

Upvotes: 2

Related Questions