expressjs / expressjs/expressjs.com

Document customizing `express.json()` size limit for specific routes

Open
#1,421 3 comments 2 reactions 0 assignees View on GitHub
4.x 5.x docs
Dominant language
MDX
Stars
5.4k
Forks
2.3k
Avg merge
2d 23m
Merged PRs (30d)
14

Description

In the following issue, it is requested to customize the body size limit for `express.json()` dynamically based on the API route:

> @marbuser (Apr 11, 2019):
> I've got some endpoints and 90% of them I want to have a limit of around '2MB'. However, there is 1 specific route, my upload route, where I want to have an upload limit of '10MB'.

- https://github.com/expressjs/express/issues/3932

There are some solutions provided in the thread, but maybe a simpler option would be this one inspired by [this Stack Overflow answer](https://stackoverflow.com/questions/74387929/increase-body-limit-for-specific-route-with-expressjs):

```js
// Parse application/json
// - limit on /upload: 5MB
// - limit on other routes: default of 100KB
app.use('/upload', express.json({ limit: '5mb' }));
app.use(/^(?!\/upload$)/, express.json());
```

This may be a nice example to document on [the `express.json()` docs section](https://expressjs.com/en/5x/api.html#express.json)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.