Kind: Function
Source: src/middleware/trailing-slash/index.ts
Part of: Middleware
Append trailing slash middleware for Hono.
Append a trailing slash to the URL if it doesn't have one. For example, /path/to/page will be redirected to /path/to/page/.
appendTrailingSlash creates Hono middleware that redirects requests whose path does not end with /. It runs before route handling so canonical slash-terminated URLs reach the matching route.
Signature
tsfunction appendTrailingSlash(options: AppendTrailingSlashOptions): MiddlewareHandler
Parameters
| Name | Type |
|---|---|
options | AppendTrailingSlashOptions |
Returns: MiddlewareHandler
Diagram
mermaidgraph LR Request[Incoming request] --> Middleware[appendTrailingSlash middleware] Middleware --> Check{Path ends with slash?} Check -- No --> Redirect[Redirect to path with slash] Check -- Yes --> Next[Continue to next middleware or route]
Usage
tsimport { Hono } from 'hono'
import { appendTrailingSlash } from 'hono/trailing-slash'
const app = new Hono()
app.use(appendTrailingSlash())
app.get('/docs/', (c) => {
return c.text('Documentation')
})
export default app
AI Coding Instructions
- Register
appendTrailingSlash()before routes that expect slash-terminated paths. - Apply the middleware at the app level when all routes should use trailing slashes, or mount it on a route prefix for narrower behavior.
- Do not add separate redirect logic inside route handlers when this middleware already handles path normalization.
- Test requests with and without a trailing slash, including paths that include query parameters.
Was this page helpful?