Appearance
预设
🌐 Presets
Hono 有几个路由,每个路由都是为特定用途设计的。你可以在 Hono 的构造函数中指定你要使用的路由。
🌐 Hono has several routers, each designed for a specific purpose. You can specify the router you want to use in the constructor of Hono.
预设 提供了常见用例,因此你不必每次都指定路由。 从所有预设中导入的 Hono 类是相同的,唯一的区别是路由。 因此,你可以互换使用它们。
hono
用法:
🌐 Usage:
ts
import { Hono } from 'hono'路由:
🌐 Routers:
ts
this.router = new SmartRouter({
routers: [new RegExpRouter(), new TrieRouter()],
})hono/quick
用法:
🌐 Usage:
ts
import { Hono } from 'hono/quick'路由:
🌐 Router:
ts
this.router = new SmartRouter({
routers: [new LinearRouter(), new TrieRouter()],
})hono/tiny
用法:
🌐 Usage:
ts
import { Hono } from 'hono/tiny'路由:
🌐 Router:
ts
this.router = new PatternRouter()我应该使用哪个预设?
🌐 Which preset should I use?
| 预设 | 适用平台 |
|---|---|
hono | 强烈推荐用于大多数使用场景。尽管注册阶段可能比 hono/quick 慢,但启动后性能表现出色。非常适合使用 Deno、Bun 或 Node.js 构建的长生命周期服务器。在 Fastly Compute 上也适用,因为在该平台上路由注册发生在应用构建阶段。对于使用 v8 隔离的环境,例如 Cloudflare Workers、Deno Deploy,此预设同样适用,因为隔离在启动后会保留一定时间。 |
hono/quick | 该预设适用于每次请求都初始化应用的环境。 |
hono/tiny | 这是最小的路由包,适合资源有限的环境。 |