Kind: Function
Source: src/middleware/trailing-slash/index.ts
Part of: Middleware
Trailing Slash Middleware for Hono.
trimTrailingSlash creates Hono middleware that redirects requests whose path ends with a trailing slash to the equivalent path without it. It keeps route URLs consistent before the request continues through the application middleware and route handlers.
Signature
tsfunction trimTrailingSlash(options: TrimTrailingSlashOptions): MiddlewareHandler
Parameters
| Name | Type |
|---|---|
options | TrimTrailingSlashOptions |
Returns: MiddlewareHandler
Diagram
mermaidgraph LR Request[Incoming request] --> Middleware[trimTrailingSlash middleware] Middleware --> Check{Path has trailing slash?} Check -->|Yes| Redirect[Redirect to trimmed path] Check -->|No| Next[Continue to next middleware or route]
Usage
tsimport { Hono } from 'hono'
import { trimTrailingSlash } from 'hono/trailing-slash'
const app = new Hono()
app.use(trimTrailingSlash())
app.get('/users', (c) => {
return c.text('User list')
})
export default app
AI Coding Instructions
- Register
trimTrailingSlash()before routes that should use normalized paths. - Import the middleware from
hono/trailing-slash. - Keep route definitions without trailing slashes when this middleware is enabled.
- Do not add separate trailing-slash route variants unless they require different behavior.
Was this page helpful?