Hono 是一个超快速、轻量级的 Web 框架,专门为 边缘计算 和现代 JavaScript/TypeScript 运行时而设计。它的名字在日语中意为”火焰”,象征着它的速度和轻量。
核心理念
1. 极致的性能
- 专为边缘环境(Cloudflare Workers、Deno、Bun 等)优化
- 极小的包体积(~14KB)
- 在边缘环境中比 Express、Fastify 等传统框架快很多
2. 跨平台兼容
1 2 3 4 5 6 7 8 9 10 11 12 13
| import { Hono } from 'hono'
const app = new Hono()
app.get('/', (c) => c.text('Hello Hono!'))
export default app Deno.serve(app.fetch) Bun.serve({ fetch: app.fetch }) export default app
|
3. 简洁的 API 设计
1 2 3 4 5 6 7 8 9 10 11 12 13
| const app = new Hono()
app.get('/users/:id', async (c) => { const id = c.req.param('id') const user = await db.users.find(id) return c.json(user) })
app.post('/users', createUser) app.put('/users/:id', updateUser) app.delete('/users/:id', deleteUser)
|
核心特性
Context 对象
所有操作围绕 Context 对象展开:
1 2 3 4 5 6 7 8 9 10 11
| app.get('/hello', (c) => { c.req c.res c.env c.executionCtx c.json({ message: 'Hello' }) c.text('Hello') c.html('<h1>Hello</h1>') c.redirect('/new-url') })
|
中间件系统
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import { logger, cors, secureHeaders } from 'hono/middleware'
app.use('*', logger()) app.use('/api/*', cors()) app.use('*', secureHeaders())
const authMiddleware = async (c, next) => { const token = c.req.header('Authorization') if (!token) return c.text('Unauthorized', 401) await next() }
app.use('/admin/*', authMiddleware)
|
验证和类型安全
1 2 3 4 5 6 7 8 9 10 11 12 13
| import { z } from 'zod' import { zValidator } from '@hono/zod-validator'
const schema = z.object({ name: z.string().min(1), email: z.string().email(), age: z.number().min(0) })
app.post('/users', zValidator('json', schema), (c) => { const data = c.req.valid('json') return c.json({ success: true }) })
|
模板渲染
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| app.get('/page', (c) => { return c.html( <html> <body> <h1>Hello Hono</h1> <p>Current time: {new Date().toLocaleTimeString()}</p> </body> </html> ) })
import { mustache } from 'hono/mustache' app.get('/template', (c) => { return c.render( mustache('<h1>Hello {{name}}</h1>', { name: 'World' }) ) })
|
平台适配器
Cloudflare Workers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
import { Hono } from 'hono' import { handle } from 'hono/cloudflare-pages'
const app = new Hono() app.get('/', (c) => c.text('Hello Cloudflare!'))
export const onRequest = handle(app)
export default app
|
Deno
1 2 3 4 5 6
| import { Hono } from 'https://deno.land/x/hono/mod.ts'
const app = new Hono() app.get('/', (c) => c.text('Hello Deno!'))
Deno.serve(app.fetch)
|
Bun
1 2 3 4 5 6 7 8 9
| import { Hono } from 'hono'
const app = new Hono() app.get('/', (c) => c.text('Hello Bun!'))
Bun.serve({ port: 3000, fetch: app.fetch })
|
Node.js
1 2 3 4 5 6 7 8 9
| import { Hono } from 'hono' import { serve } from '@hono/node-server'
const app = new Hono() app.get('/', (c) => c.text('Hello Node.js!'))
serve(app, (info) => { console.log(`Server is running on http://localhost:${info.port}`) })
|
Vercel/Netlify Edge
1 2 3 4 5 6 7
| import { Hono } from 'hono'
const app = new Hono() app.get('/', (c) => c.text('Hello Edge!'))
export default app
|
高级特性
路由分组和组合
1 2 3 4 5 6 7 8 9 10 11
| const bookRoutes = new Hono() bookRoutes.get('/', (c) => c.text('List books')) bookRoutes.get('/:id', (c) => c.text(`Book ${c.req.param('id')}`))
const apiRoutes = new Hono() .route('/books', bookRoutes) .route('/authors', authorRoutes)
app.route('/api', apiRoutes)
|
WebSocket 支持
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import { Hono } from 'hono' import { upgradeWebSocket } from 'hono/ws'
const app = new Hono()
app.get('/ws', upgradeWebSocket((c) => ({ onMessage(event, ws) { console.log('Message:', event.data) ws.send('Hello Client!') }, onClose() { console.log('Connection closed') } })))
|
OpenAPI 集成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import { OpenAPIHono } from '@hono/zod-openapi'
const app = new OpenAPIHono()
app.openapi( { method: 'get', path: '/users/{id}', responses: { 200: { description: 'Get user by ID', content: { 'application/json': { } } } } }, (c) => { const id = c.req.param('id') return c.json({ id, name: 'John' }) } )
app.doc('/doc', { openapi: '3.0.0', info: { title: 'My API', version: '1.0.0' } })
|
性能对比
| 框架 |
冷启动时间 |
内存占用 |
包大小 |
适合场景 |
| Hono |
~5ms |
~5MB |
~14KB |
边缘计算 |
| Express |
~100ms |
~30MB |
~200KB |
传统服务器 |
| Fastify |
~50ms |
~20MB |
~150KB |
高性能 API |
| tRPC |
~20ms |
~15MB |
~100KB |
全栈类型安全 |
使用场景
非常适合
- ✅ 边缘函数 - Cloudflare Workers、Vercel Edge
- ✅ API 网关 - 轻量级代理和路由
- ✅ 微服务 - 快速构建小型服务
- ✅ Jamstack 后端 - 配合静态站点
- ✅ 实时应用 - WebSocket + 边缘计算
- ✅ Serverless 函数 - 冷启动快
不太适合
- ❌ 传统 MVC 应用 - 需要完整框架特性
- ❌ 企业级复杂应用 - 可能需要更多内置功能
- ❌ 需要大量第三方中间件
生态系统
官方包
hono/middleware - 常用中间件
hono/validator - 验证工具
@hono/zod-openapi - OpenAPI 集成
hono/jsx - JSX 渲染
hono/websocket - WebSocket 支持
社区适配器
- Firebase Functions
- AWS Lambda
- Supabase Functions
- 各种数据库和 ORM 集成
简单示例:完整的 API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import { Hono } from 'hono' import { logger } from 'hono/logger' import { cors } from 'hono/cors' import { serve } from '@hono/node-server'
const app = new Hono()
app.use('*', logger()) app.use('/api/*', cors())
app.get('/', (c) => c.text('Hello Hono!'))
app.get('/api/users', async (c) => { const users = await db.users.findMany() return c.json(users) })
app.post('/api/users', async (c) => { const data = await c.req.json() const user = await db.users.create({ data }) return c.json(user, 201) })
app.get('/api/users/:id', async (c) => { const id = c.req.param('id') const user = await db.users.findUnique({ where: { id } }) return user ? c.json(user) : c.text('Not found', 404) })
app.onError((err, c) => { console.error(err) return c.json({ error: 'Internal Server Error' }, 500) })
app.notFound((c) => { return c.text('Route not found', 404) })
if (import.meta.main) { serve(app, (info) => { console.log(`🚀 Server running on http://localhost:${info.port}`) }) }
export default app
|
总结
Hono 的主要优势:
- 性能卓越 - 专为边缘环境优化
- 轻量简洁 - 极小的包体积和简单的 API
- 平台无关 - 一次编写,到处运行
- 现代特性 - 一流的 TypeScript 支持,内置验证等
选择 Hono 的场景:
- 你需要部署到边缘平台(Cloudflare、Vercel Edge 等)
- 关注冷启动时间和响应速度
- 想要极简的 API 框架
- 项目需要跨多个 JavaScript 运行时
如果你正在构建边缘优先的现代 Web 应用,Hono 是一个非常优秀的选择!