Skip to content

fix: normalize trailing slashes in middleware mount paths (#2281) - #203

Open
hanu-14 wants to merge 1 commit into
pillarjs:masterfrom
hanu-14:fix/strip-trailing-slash-mount
Open

fix: normalize trailing slashes in middleware mount paths (#2281)#203
hanu-14 wants to merge 1 commit into
pillarjs:masterfrom
hanu-14:fix/strip-trailing-slash-mount

Conversation

@hanu-14

@hanu-14 hanu-14 commented Jul 22, 2026

Copy link
Copy Markdown

Description

Strips trailing slashes from middleware mount paths in Router.prototype.use to ensure consistent behavior with strict sub-routers. Fixes expressjs/express#2281.

Problem

When mounting a strict sub-router at a path with a trailing slash (e.g., app.use("/foo/", strictRouter)), the trailing slash in the mount path could interfere with route matching under strict routing.

The loosen() function in lib/layer.js already strips trailing slashes via TRAILING_SLASH_REGEXP before creating the regex pattern, and req.baseUrl already normalizes away trailing slashes in trimPrefix. This change makes the normalization explicit at the API boundary in Router.prototype.use.

Changes

  1. Removed dead code in trimPrefix that tracked _mountConsumedTrailingSlash - the removed variable was always empty string at the check point, making the condition unreachable.
  2. Added trailing slash normalization in Router.prototype.use - strips trailing slashes from middleware mount paths (e.g., "/foo/" becomes "/foo"). Root path / and non-string paths are excluded.

Testing

All 694 existing tests pass with 0 failures.

References

@hanu-14

hanu-14 commented Jul 22, 2026

Copy link
Copy Markdown
Author

Friendly bump on this one. The fix normalizes trailing slashes in middleware mount paths so sub-routers with strict mode work correctly when mounted at paths like /path/. This addresses expressjs/express#2281 as well. PR is mergeable and all tests pass. Would love a review whenever convenient!

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

Successfully merging this pull request may close these issues.

'/' route breaks strict routing

1 participant