blog.dopana

Back

Hono là web framework siêu nhẹ (~14KB), siêu nhanh, chạy trên mọi runtime: Cloudflare Workers, Deno, Bun, Node.js, Lambda.

import { Hono } from 'hono'

const app = new Hono()

app.get('/', (c) => c.text('Hello Hono!'))
app.get('/json', (c) => c.json({ message: 'Hello!' }))
app.post('/api/data', async (c) => {
  const body = await c.req.json()
  return c.json({ received: body })
})

export default app
ts

Tại Sao Hono?#

So Sánh Runtime#

ExpressFastifyHono
Kích thước~200KB~150KB~14KB
Cloudflare Workers
Deno
Bun
Node
AWS Lambda
RouterRegexpTrie/RegexpTrie (nhanh nhất)
TypeScriptOKTốtXuất sắc

Hono không chỉ nhỏ — nó còn có router nhanh nhất trong các web framework JS (dùng Trie tree, không regexp).

Tốc Độ Router#

Framework          Req/s
Hono (Trie)        ~280k
Fastify            ~220k
Express             ~15k
plaintext

Với edge computing, mỗi microsecond quan trọng — Hono được tối ưu cho cold start.

Cài Đặt#

mkdir hono-app && cd hono-app
npm init -y
npm install hono
npx tsc --init
bash

Cloudflare Workers#

npm create cloudflare@latest my-worker -- --template hono
cd my-worker
npm run dev
bash

Bun#

bun init -y
bun add hono
bash

Router#

Middleware#

Hono có middleware system giống Express nhưng nhẹ hơn:

// Built-in middleware
import { cors } from 'hono/cors'
import { logger } from 'hono/logger'
import { jwt } from 'hono/jwt'
import { etag } from 'hono/etag'
import { prettyJSON } from 'hono/pretty-json'
import { timeout } from 'hono/timeout'

const app = new Hono()

app.use('*', logger())
app.use('*', cors({ origin: 'https://example.com' }))
app.use('/api/*', timeout(5000))
app.use('*', prettyJSON())
app.use('/static/*', etag())
ts

JWT Auth#

import { jwt } from 'hono/jwt'

app.use('/admin/*', jwt({
  secret: 'my-secret-key',
}))

app.get('/admin/dashboard', (c) => {
  const payload = c.get('jwtPayload')
  return c.json({ admin: payload })
})
ts

Custom Middleware#

Validation với Zod#

RPC — Type-Safe Client#

Giống tRPC, Hono có RPC mode cho type safety từ server đến client:

Không codegen, không schema riêng — TypeScript inference thuần.

Streaming & SSE#

Testing#

import { Hono } from 'hono'
import { test, describe, expect } from 'bun:test'

const app = new Hono()
app.get('/hello', (c) => c.json({ message: 'Hello!' }))

describe('GET /hello', () => {
  test('should return 200', async () => {
    const res = await app.request('/hello')
    expect(res.status).toBe(200)
    expect(await res.json()).toEqual({ message: 'Hello!' })
  })
})
ts

Không cần supertest — app.request() mock HTTP luôn.

Deploy#

Cloudflare Workers#

// wrangler.jsonc
{
  "name": "my-worker",
  "main": "src/index.ts",
  "compatibility_date": "2026-07-01"
}
ts
npx wrangler deploy
bash

Bun#

import { Hono } from 'hono'
import { serve } from 'bun'

const app = new Hono()
app.get('/', (c) => c.text('Bun + Hono'))

serve(app.fetch)
ts

Deno#

import { Hono } from 'jsr:@hono/hono'

const app = new Hono()
app.get('/', (c) => c.text('Deno + Hono'))

Deno.serve(app.fetch)
ts

Node.js#

npm install @hono/node-server
bash
import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const app = new Hono()
app.get('/', (c) => c.text('Node + Hono'))

serve(app, { port: 3000 })
ts

Khi Nào Dùng Hono?#

✅ Nên dùng#

  • Edge API — Cloudflare Workers, Deno Deploy
  • Serverless — cold start quan trọng
  • Microservices nhẹ — muốn framework tối giản
  • Multi-runtime — code chạy cả Workers + Node + Bun
  • API nhanh — cần throughput cao

❌ Không nên#

  • Full-featured web app — cần session, template engine, ORM tích hợp (chọn NextJS/Nuxt)
  • Legacy Express project — migration không đáng
  • Cần nhiều plugin cộng đồng — Hono còn mới, ecosystem nhỏ hơn Express/Fastify

So Sánh Chi Tiết#

Hono vs Express#

// Express
const express = require('express')
const app = express()
app.get('/api/users/:id', async (req, res) => {
  const user = await db.findUser(req.params.id)
  res.json(user)
})

// Hono (giống, nhưng nhẹ hơn, edge-native)
import { Hono } from 'hono'
const app = new Hono()
app.get('/api/users/:id', async (c) => {
  const user = await db.findUser(c.req.param('id'))
  return c.json(user)
})
ts

Hono API quen thuộc nếu bạn biết Express, nhưng được thiết kế cho edge: không dùng Node API, dùng Web API chuẩn (Request/Response).

Hono vs Fastify#

// Fastify — plugin system mạnh, schema-based validation
fastify.post('/users', {
  schema: {
    body: {
      type: 'object',
      properties: { name: { type: 'string' } }
    }
  }
}, handler)

// Hono — Zod trực tiếp, không cần schema language
app.post('/users', zValidator('json', z.object({
  name: z.string()
})), handler)
ts

Fastify dùng JSON Schema (Ajv). Hono dùng Zod hoặc custom parser — linh hoạt hơn, TypeScript-native hơn.

Helper phổ biến#

Tổng Kết#

Hono là web framework phù hợp nhất cho kỷ nguyên edge computing hiện tại.

Ưu điểmNhược điểm
~14KB — siêu nhẹEcosystem còn nhỏ
Nhanh nhất (Trie router)Chưa có nhiều template engine
Chạy mọi runtimeKhông ORM tích hợp
TypeScript-first
RPC type-safe built-in
Cold start nhanh
# Chạy app Hono đầu tiên trong 30s
npm create cloudflare@latest my-api -- --template hono
cd my-api
npm run dev
bash

Hono viết tắt của “Honestly Only Needs One” — đúng nghĩa: một framework cho mọi runtime.

Tài liệu tham khảo#