fix: enforce trailing slash in mount path for .use() layers by DukeDeSouth · Pull Request #189 · pillarjs/router

@DukeDeSouth @cursoragent

When a middleware is mounted with a trailing slash (e.g. `.use('/api/', handler)`),
requests without the trailing slash (e.g. `GET /api`) should not be dispatched to
the handler. Previously, `loosen()` stripped the trailing slash from the mount path
for matching purposes, which caused requests without the trailing slash to match
and receive an artificial `'/'` as `req.url`, breaking strict routing in sub-routers.

This fix adds a check in `trimPrefix()` that compares the request path against the
original (un-loosened) mount path. When the original mount path ends with a trailing
slash that was stripped by `loosen()`, the request must also include that trailing
slash to proceed. Otherwise, the layer is skipped.

Fixes: expressjs/express#2281
Co-authored-by: Cursor <cursoragent@cursor.com>