Skip to content

Trailing Slash 미들웨어

이 미들웨어는 GET 요청 시 URL의 Trailing Slash(마지막 슬래시)를 처리한다.

appendTrailingSlash는 컨텐츠를 찾지 못한 경우 Trailing Slash를 추가한 URL로 리다이렉트한다. 또한 trimTrailingSlash는 Trailing Slash를 제거한다.

Import

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.