Skip to content

预设路由器

Hono 提供多种路由器,分别针对不同的使用场景进行了优化。 你可以在创建 Hono 实例时通过构造函数选择具体的路由器。

为便于常见场景的使用,框架内置了若干预设(Preset),这样就无需在每次实例化时都手动指定路由器。 所有预设导出的 Hono 类本质一致,差异仅在于默认绑定的路由器,因此它们可以互换使用。

hono

用法:

ts
import { 
Hono
} from 'hono'

路由器:

ts
this.router = new SmartRouter({
  routers: [new RegExpRouter(), new TrieRouter()],
})

hono/quick

用法:

ts
import { 
Hono
} from 'hono/quick'

路由器:

ts
this.router = new SmartRouter({
  routers: [new LinearRouter(), new TrieRouter()],
})

hono/tiny

用法:

ts
import { 
Hono
} from 'hono/tiny'

路由器:

ts
this.router = new PatternRouter()

应该选择哪种预设?

  • hono:推荐用于大多数场景。虽然注册阶段比 hono/quick 略慢,但启动后性能优异,适用于基于 DenoBunNode.js 的长生命周期服务器。在 Cloudflare WorkersDeno Deploy 等使用 V8 隔离的环境中同样表现稳定,因为实例在启动后会保持一段时间。
  • hono/quick:面向每次请求都会重新初始化应用的环境,例如 Fastly Compute,因此更适合这些平台。
  • hono/tiny:体积最小的路由器包,适用于资源受限的运行环境。

Released under the MIT License.