forked from honojs/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(trailing-slash): add Trailing Slash Middleware (honojs#284)
- Loading branch information
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Tailing Slash Middleware | ||
|
||
This middleware resolves the handling of Trailing Slashes in GET requests. | ||
|
||
`appendTrailingSlash` redirects to the URL to which it added the Tailing Slash if the content was not found. Also, `trimTrailingSlash` will remove the Tailing Slash. | ||
|
||
## Import | ||
|
||
::: code-group | ||
|
||
```ts [npm] | ||
import { Hono } from 'hono' | ||
import { appendTrailingSlash, trimTrailingSlash } from 'hono/trailing-slash' | ||
``` | ||
|
||
```ts [Deno] | ||
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 to `/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 to `/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`. |