跳至內容

尾部斜線中間件

此中間件處理 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 時,將會啟用。

根據 MIT 許可證發布。