Skip to content

尾部斜杠中间件

¥Trailing Slash Middleware

此中间件处理 GET 请求中 URL 中的尾部斜杠。

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

如果未找到内容,appendTrailingSlash 会将 URL 重定向到添加了尾部斜杠的位置。此外,trimTrailingSlash 将删除尾部斜杠。

¥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'

用法

¥Usage

/about/me 的 GET 请求重定向到 /about/me/ 的示例。

¥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'))

/about/me/ 的 GET 请求重定向到 /about/me 的示例。

¥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

当请求方法是 GET 且响应状态是 404 时,它将被启用。

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

Hono v4.7 中文网 - 粤ICP备13048890号