正在加载,请稍候…

Hono.js: Ultra-Fast APIs for Edge Runtimes (Cloudflare Workers, Deno, Bun)

Build blazing-fast APIs with Hono.js that run on edge runtimes — routing, middleware, Zod validation, RPC client, deployment to Cloudflare Workers and Deno Deploy.

Why Hono for the Edge?

Hono is a small, fast web framework designed for edge runtimes. It runs on Cloudflare Workers, Deno Deploy, Bun, Node.js, and more — same code, any runtime.

Quick Start

npm create hono@latest my-app
cd my-app && npm install
import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'
import { z } from 'zod'

const app = new Hono()

app.get('/', (c) => c.text('Hello Hono!'))

const userSchema = z.object({
  name: z.string().min(2),
  email: z.string().email(),
})

app.post('/users', zValidator('json', userSchema), async (c) => {
  const data = c.req.valid('json')
  // data is typed!
  const user = await createUser(data)
  return c.json(user, 201)
})

app.get('/users/:id', async (c) => {
  const id = c.req.param('id')
  const user = await getUserById(id)
  if (!user) return c.notFound()
  return c.json(user)
})

export default app

Middleware

import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { logger } from 'hono/logger'
import { timing } from 'hono/timing'
import { bearerAuth } from 'hono/bearer-auth'

const app = new Hono()

// Built-in middleware
app.use('*', logger())
app.use('*', cors())
app.use('*', timing())

// Auth middleware on specific routes
app.use('/api/*', bearerAuth({ token: process.env.API_TOKEN! }))

Cloudflare Workers (with D1, KV, R2)

// worker.ts
import { Hono } from 'hono'

type Bindings = {
  DB: D1Database        // SQLite at the edge
  KV: KVNamespace       // Key-value store
  BUCKET: R2Bucket      // Object storage
}

const app = new Hono<{ Bindings: Bindings }>()

app.get('/posts', async (c) => {
  const { results } = await c.env.DB.prepare(
    'SELECT * FROM posts ORDER BY created_at DESC LIMIT 20'
  ).all()
  return c.json(results)
})

app.get('/cache/:key', async (c) => {
  const key = c.req.param('key')
  const value = await c.env.KV.get(key)
  if (!value) return c.notFound()
  return c.text(value)
})

export default app

RPC Client (type-safe like tRPC)

// server
const routes = app
  .get('/users', (c) => c.json({ users: [] }))
  .post('/users', zValidator('json', userSchema), (c) => {
    return c.json({ id: '1', ...c.req.valid('json') })
  })

export type AppType = typeof routes

// client (frontend or another service)
import { hc } from 'hono/client'

const client = hc<AppType>('https://api.example.com')
const res = await client.users.$get()
const { users } = await res.json() // Fully typed!

Performance Numbers

Hono is among the fastest Node.js frameworks:

  • ~200k req/s on Cloudflare Workers
  • ~140k req/s on Bun
  • Startup time: < 1ms (perfect for cold starts)