Skip to content

Trailing Slash 中间件

该中间件用于在 GET 请求中处理 URL 末尾的斜杠。

如果资源未找到,appendTrailingSlash 会把 URL 重定向到带斜杠的版本;trimTrailingSlash 则会移除末尾斜杠。

导入

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

用法

示例:将 /about/me 的 GET 请求重定向到 /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'))

示例:将 /about/me/ 的 GET 请求重定向到 /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'))

注意

仅在请求方法为 GET 且响应状态码为 404 时生效。

Released under the MIT License.