Skip to content

Trailing Slash Middleware

This middleware handles Trailing Slash in the URL on a GET request.

appendTrailingSlash redirects the URL to which it added the Trailing Slash if the content was not found. Also, trimTrailingSlash will remove the Trailing Slash.

Import

ts
import { Hono } from 'hono'
import { appendTrailingSlash, trimTrailingSlash } from 'hono/trailing-slash'
ts
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { appendTrailingSlash, trimTrailingSlash } from 'https://deno.land/x/hono/middleware.ts'

Usage

Example of redirecting a GET request of /about/me to /about/me/.

ts
import { Hono } from 'hono'
import { appendTrailingSlash } from 'hono/trailing-slash'

const app = new Hono({ strict: true })

app.use(appendTrailingSlash())
app.get('/about/me/', (c) => c.text('With Trailing Slash'))

Example of redirecting a GET request of /about/me/ to /about/me.

ts
import { Hono } from 'hono'
import { trimTrailingSlash } from 'hono/trailing-slash'

const app = new Hono({ strict: true })

app.use(trimTrailingSlash())
app.get('/about/me', (c) => c.text('Without Trailing Slash'))

Note

It will be enabled when the request method is GET and the response status is 404.

Released under the MIT License.