Hono Web Framework

repository·main·Indexed 12 days ago

https://github.com/honojs/hono

A small, simple, and ultrafast web framework built on Web Standards. Designed to run on any JavaScript runtime, including Cloudflare Workers, Deno, Bun, and Node.js, Hono is optimized for edge computing and serverless environments. Version 4.13.1 features high-performance routers, zero dependencies in its tiny preset, and first-class TypeScript support.

Tokens
41.6K
Snippets
159
Records
203
Agent score
92%

What's inside Hono

  1. Overview of Hono Features

    main

    Hono is designed for speed, small footprint, and multi-runtime compatibility:

    • Ultrafast: Uses high-performance routers like RegExpRouter instead of linear loops.
    • Lightweight: The hono/tiny preset is under 12kB. It has zero dependencies and relies solely on Web Standard APIs.
    • Multi-runtime: The same codebase runs on Cloudflare Workers, Fastly Compute, Deno, Bun, AWS Lambda, Lambda@Edge, and Node.js.
    • Batteries Included: Provides built-in middleware, support for custom middleware, and third-party middleware.
    • Delightful DX: Offers clean APIs and first-class TypeScript support.
  2. Create third-party middleware

    main

    Third-party middleware is kept separate from the Hono core. This allows middleware to depend on other libraries or target specific environments (e.g., Cloudflare Workers, GraphQL, Firebase, Sentry) without bloating the core package.

    If you want to contribute middleware to the official ecosystem:

    1. Middleware can be distributed under the @honojs namespace.
    2. The honojs/middleware monorepo manages these official extensions.
    3. Before starting, create an issue to discuss your proposed middleware.
  3. Run router benchmarks

    main

    This benchmark suite compares the performance of various HTTP routers, including find-my-way, express, koa-router, koa-tree-router, trek-router, @medley/router, and Hono's own RegExpRouter and TrieRouter.

    To run the benchmarks, first install the dependencies using Bun, then execute the specific command for your target runtime.

    # Install dependencies
    bun install --frozen-lockfile
    
    # Run benchmarks for Node.js
    bun run bench:node
    
    # Run benchmarks for Bun
    bun run bench:bun
  4. Migrate from v2.0.9 to v2.1.0: Request Body Parsing

    main

    In v2.1.0, c.req.parseBody() was restricted to parsing FormData with content types multipart/form or application/x-www-form-urlencoded. It no longer parses JSON, text, or ArrayBuffers. Use the specific methods for those types instead.

    // multipart/form or application/x-www-form-urlencoded
    const data = await c.req.parseBody()
    
    // For other types
    const jsonData = await c.req.json() // for JSON body
    const text = await c.req.text() // for text body
    const arrayBuffer = await c.req.arrayBuffer() // for ArrayBuffer
  5. Migrate from v2.7.8 to v3.0.0

    main

    Version 3.0.0 introduced significant breaking changes:

    • HonoRequest: c.req is now a HonoRequest object rather than a standard Request. To access the underlying standard Request object, use c.req.raw.
    • StaticRouter: This is obsolete and can no longer be used.
    • Validator: The previous Validator Middleware is obsolete. Use the updated API in hono/validator.
    • serveStatic: The standalone serveStatic middleware is obsolete. You must now use the serveStatic provided by your specific runtime adapter (e.g., hono/cloudflare-workers, hono/bun, or hono/deno).
    • Generics for new Hono: When defining generics for the Hono constructor, you must use type instead of interface.
    // Accessing standard Request in v3+
    app.post('/', async (c) => {
      const metadata = c.req.raw.cf?.hostMetadata?
      ...
    })
    
    // Using type for Generics
    type Bindings = {
      TOKEN: string
    }
    
    const app = new Hono<{ Bindings: Bindings }>()
  6. Migrate from v1.6.4 to v2.0.0: Deno Middleware Imports

    main

    In Deno, do not import middleware from hono/mod.ts as it does not export them. Instead, import middleware from hono/middleware.ts.

    import { Hono } from 'https://deno.land/x/hono/mod.ts'
    import { poweredBy, basicAuth } from 'https://deno.land/x/hono/middleware.ts'
  7. Migrate from v1.6.4 to v2.0.0: Cookie and Body Parsing

    main

    The cookie and body-parse middlewares are obsolete. Use the built-in methods on the request object instead:

    • Cookies: Use c.req.cookie(name) to parse and c.cookie(name, value) to set cookies.
    • Body Parsing: Use c.req.parseBody() to parse request bodies (specifically for form data).
    // Parse cookie
    app.get('/entry/:id', (c) => {
      const value = c.req.cookie('name')
      ...
    })
    
    // Set cookie
    app.get('/', (c) => {
      c.cookie('delicious_cookie', 'choco')
      return c.text('Do you like cookie?')
    })
    
    // Parse Request body
    app.post('', (c) => {
      const body = c.req.parseBody()
      ...
    })
  8. Migrate from v3.12.x to v4.0.0

    main

    Version 4.0.0 introduced several breaking changes and removals of deprecated features. Key updates include:

    • Adapters: AWS Lambda users should use ApiGatewayRequestContextV2 instead of LambdaFunctionUrlRequestContext. Next.js users should use hono/vercel instead of hono/nextjs.
    • Context Methods: c.jsonT() is replaced by c.json(). c.stream() and c.streamText() are replaced by stream() and streamText() from hono/streaming. c.env() is replaced by getRuntimeKey() in hono/adapter.
    • Hono Methods: app.showRoutes() is now showRoutes() in hono/dev. app.routerName is now getRouterName() in hono/dev. app.handleEvent() is replaced by app.fetch(). app.head() is no longer explicitly required as app.get() handles it.
    • HonoRequest: req.cookie() is replaced by getCookie() in hono/cookie. Other properties like headers(), body(), signal(), etc., should now be accessed via req.raw (e.g., req.raw.headers).
    • Cloudflare Workers: serveStatic in the Cloudflare Workers adapter now requires a manifest option.
    import manifest from '__STATIC_CONTENT_MANIFEST'
    
    // ...
    
    app.use('/static/*', serveStatic({ root: './assets', manifest }))
  9. Run Hono HTTP Benchmarks locally

    main

    To run the HTTP performance benchmarks locally to compare the main vs current versions of Hono, navigate to the benchmark directory and use bun to execute the benchmark script. This requires bun and bombardier to be installed on your system.

    cd benchmarks/http-server
    bun run benchmark.ts