Skip to content

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

Open

Description

@karlhorky

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

Activity

  1. itsalb3rt commented on Dec 31, 2023

    @itsalb3rt

    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());
  2. added
    5.x
    4.x
    docs
    on Dec 9, 2024
  3. jonchurch commented on Sep 22, 2025

    @jonchurch
    Member

    The root issue expressjs/express#3932 that inspired this issue is really about understanding middleware ordering and targeting specific routes with specific middleware, not about bodyParser.limit setting really.

  4. karlhorky commented on Sep 23, 2025

    @karlhorky
    Author

    @jonchurch hmm not sure what you mean.

    I understood expressjs/express#3932 to be about "how can I configure the /upload route with a certain limit and non-/upload routes with a different limit?" since that is the case provided in that issue description at the top of the page.

    I think my issue #1421 doesn't subvert that original wish.

    Do you disagree with either of those things?

  5. added a commit that references this issue on Nov 24, 2025
    333a9b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    4.x5.xdocs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions