Skip to main content

Import

trimTrailingSlash

Usage

Options

boolean
default:false
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)
  • alwaysRedirect mode: redirects before handler execution

appendTrailingSlash

Usage

Options

boolean
default:false
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)
  • alwaysRedirect mode: redirects before handler execution

Usage Notes

  • Do not use both trimTrailingSlash and appendTrailingSlash together
  • Middleware should be applied before route definitions
  • For wildcard routes, use alwaysRedirect: true option
  • Both preserve full URL including query parameters