Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
karlhorky opened this issue May 25, 2023 · 1 comment
Open

Comments

@karlhorky
Copy link

karlhorky commented May 25, 2023

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'.

There are some solutions provided in the thread, but maybe a simpler option would be this one inspired by this Stack Overflow answer:

// 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

@dougwilson dougwilson transferred this issue from expressjs/express May 25, 2023
@itsalb3rt
Copy link

For future people here, I have a problem because I forgot my pre-fix API structure, all my endpoints start with API

for example api/upload/

import bodyParser from 'body-parser';
// others imports

// more code...
app.use('/api/posts',bodyParser.json({ limit: '50mb' }));
app.use(/^(?!\/api\/\/posts$)/, bodyParser.json());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants