🌐 Web Development

Hono

Hono (η‚Ž, flame)

Hono is an ultra-lightweight web framework optimized for Edge runtimes. It supports various runtimes including Cloudflare Workers, Deno, Bun, and Node.js, and its Express-like API makes it quick to learn. Its very small bundle size makes it ideal for serverless environments.

πŸ“– Detailed Explanation

Hono (η‚Ž, Japanese for "flame") is an ultra-lightweight web framework based on TypeScript that appeared in 2022. Its core library is only about 12KB (gzip), so it runs comfortably within the 1MB limit of Cloudflare Workers. Built on the Web Standard API (Request, Response), it offers excellent portability across runtimes.

It provides a routing API similar to Express, but internally it uses a Trie-based router that delivers faster matching performance. You define routes with familiar patterns such as app.get() and app.post(), and generate responses with context helpers such as c.json() and c.html(). TypeScript support is excellent, so route parameters and request/response types are inferred automatically.

The middleware ecosystem is rich. Official middleware is provided for CORS, JWT authentication, logging, caching, compression, and more, and it is compatible with the Express middleware style. Using the Zod Validator middleware, you can handle request body validation and type inference at the same time, which improves type safety.

Multi-runtime support is its core strength. The same code runs on Cloudflare Workers, Deno Deploy, AWS Lambda, Vercel Edge, Bun, and Node.js. On Cloudflare Workers, you can directly access bindings such as KV, D1, and R2, and platform-specific features of each platform can also be used through hono/adapter.

πŸ’» Code Example

import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { jwt } from 'hono/jwt';
import { zValidator } from '@hono/zod-validator';
import { z } from 'zod';

// Hono μ•± 생성 (νƒ€μž… μ•ˆμ „ν•œ ν™˜κ²½ λ³€μˆ˜)
type Env = {
  Bindings: {
    DB: D1Database;
    JWT_SECRET: string;
  };
};

const app = new Hono<Env>();

// 미듀웨어 적용
app.use('*', cors());
app.use('/api/*', jwt({ secret: (c) => c.env.JWT_SECRET }));

// 기본 라우트
app.get('/', (c) => {
  return c.json({ message: 'Hello, Hono!' });
});

// νŒŒλΌλ―Έν„°κ°€ μžˆλŠ” 라우트 (νƒ€μž… μžλ™ μΆ”λ‘ )
app.get('/users/:id', async (c) => {
  const id = c.req.param('id'); // string으둜 μΆ”λ‘ 
  const user = await c.env.DB
    .prepare('SELECT * FROM users WHERE id = ?')
    .bind(id)
    .first();

  if (!user) {
    return c.json({ error: 'User not found' }, 404);
  }
  return c.json(user);
});

// Zod μŠ€ν‚€λ§ˆλ‘œ μš”μ²­ 검증
const createUserSchema = z.object({
  name: z.string().min(2),
  email: z.string().email(),
});

app.post(
  '/users',
  zValidator('json', createUserSchema),
  async (c) => {
    const body = c.req.valid('json'); // νƒ€μž… μžλ™ μΆ”λ‘ 

    const result = await c.env.DB
      .prepare('INSERT INTO users (name, email) VALUES (?, ?)')
      .bind(body.name, body.email)
      .run();

    return c.json({ id: result.lastRowId }, 201);
  }
);

// κ·Έλ£Ή λΌμš°νŒ…
const apiRoutes = new Hono<Env>()
  .get('/posts', (c) => c.json([]))
  .post('/posts', (c) => c.json({ created: true }));

app.route('/api', apiRoutes);

// μ—λŸ¬ 핸듀링
app.onError((err, c) => {
  console.error(err);
  return c.json({ error: 'Internal Server Error' }, 500);
});

// Cloudflare Workers μ—”νŠΈλ¦¬ν¬μΈνŠΈ
export default app;

// Node.jsμ—μ„œ μ‹€ν–‰ μ‹œ
// import { serve } from '@hono/node-server';
// serve(app, { port: 3000 });

πŸ—£οΈ Examples of Practical Conversations

πŸ‘¨β€πŸ’» Frontend Developer
"I'm trying to put a simple API server on Cloudflare Workers, but using Express makes the bundle too large. Is there a lightweight alternative?"
πŸ‘©β€πŸ’» Full-Stack Developer
"Try Hono. The core is only 12KB, and its API is similar to Express, so it's quick to get used to. TypeScript support is great too, and if you integrate Zod, request validation and type inference happen automatically. You can also use Workers' D1 and KV bindings cleanly."
πŸ‘¨β€πŸ’Ό Technical interviewer
"What are the main differences between Hono and Express?"
πŸ‘¨β€πŸ’» Candidate
"Express is Node.js-only and uses req/res objects, but Hono uses the Web Standard Request/Response, so it also works on Edge runtimes. Its bundle size is much smaller, its TypeScript support is excellent, and its router performance is faster as well. However, compared to Express's vast ecosystem, it still has fewer middleware options."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Cautions

πŸ”— Related Terms

πŸ“š Learn More