Import
trimTrailingSlash
Usage
Options
If
true, the middleware will always redirect requests with a trailing slash before executing handlers. This is useful for routes with wildcards (*).If false (default), it will only redirect when the route is not found (404).Signature
Examples
Basic usage
With wildcard routes
Preserve query strings
Behavior
- Only affects GET and HEAD requests
- Root path
/is never redirected - Returns 301 Permanent Redirect
- Preserves query strings and hash fragments
- Default mode: redirects only on 404 (route not found)
alwaysRedirectmode: redirects before handler execution
appendTrailingSlash
Usage
Options
If
true, the middleware will always redirect requests without a trailing slash before executing handlers. This is useful for routes with wildcards (*).If false (default), it will only redirect when the route is not found (404).Signature
Examples
Basic usage
With wildcard routes
Preserve query strings
Behavior
- Only affects GET and HEAD requests
- Returns 301 Permanent Redirect
- Preserves query strings and hash fragments
- Default mode: redirects only on 404 (route not found)
alwaysRedirectmode: redirects before handler execution
Usage Notes
- Do not use both
trimTrailingSlashandappendTrailingSlashtogether - Middleware should be applied before route definitions
- For wildcard routes, use
alwaysRedirect: trueoption - Both preserve full URL including query parameters