Search across all documentation pages
10 examples to build APIs with Hono on Node.js - 7 basic and 3 intermediate.
mkdir hono-api && cd hono-api
npm init -y
npm pkg set type=module
npm install hono @hono/node-server
npm install -D typescript@5.6 tsxFor runtime portability decisions, see Hono on Node vs Edge.
import { Hono } from "hono";
import { serve } from "@hono/node-server";
const app = new Hono();
app.get("/", (c) => c.text("Hello from Hono\n"
c is the Context object with helpers for response typesapp.fetch is a Web Standard fetch handler@hono/node-server bridges Hono to Node HTTPimport { Hono } from "hono";
const app = new Hono();
app.get("/users", (c) => {
return c.json([{ id: 1, name: "Ada Lovelace" }]);
});c.json() sets Content-Type: application/jsonimport { Hono } from "hono";
const app = new Hono();
app.get("/users/:id", (c) => {
const id = c.req.param("id");
const include
c.req.param() for path paramsc.req.query() for query stringsimport { Hono } from "hono";
const app = new Hono();
app.post("/users", async (c) => {
const body = await c.req.json<{ name:
c.req.json() parses body asynchronouslyc.json()import { Hono } from "hono";
import { logger } from "hono/logger";
const app = new Hono();
app.use("*", logger());
app.use("/api/*", async (
app.use(path, middleware) for path-scoped middlewareawait next() to continue the chainlogger() middleware for request loggingimport { Hono } from "hono";
const users = new Hono();
users.get("/", (c) => c.json([]));
users.get("/:id", (c) => c.
app.route(prefix, subApp) mounts route groupsimport { Hono } from "hono";
import { HTTPException } from "hono/http-exception";
const app = new Hono();
app.get("/users/:id", (c) => {
const id = c.req.param
HTTPException for operational errors with status codesapp.onError() for centralized error handlingimport { Hono } from "hono";
import { createMiddleware } from "hono/factory";
type Variables = { userId: string };
const app = new Hono<{ Variables: Variables }>();
const
Variables type for c.set() / c.get()createMiddleware for typed middleware factoriesimport { Hono } from "hono";
import { cors } from "hono/cors";
import { secureHeaders } from "hono/secure-headers";
const app = new Hono();
app.use("*", cors({ origin: "https://app.example.com" }));
helmet dependency neededimport { Hono } from "hono";
import { zValidator } from "@hono/zod-validator";
import { z } from "zod";
const app = new Hono();
const schema = z.object({
name: z.string
@hono/zod-validator integrates Zod with Honoc.req.valid("json") returns typed, validated dataHono for edge portability, tiny footprint, or Web Standards alignment. Express/Fastify for larger Node ecosystem. See Framework Selection Checklist.
Yes via @hono/node-server. Same Hono code can deploy to Cloudflare Workers.
Yes. Used in production on Cloudflare Workers and Node. Smaller ecosystem than Express/Fastify.
~14KB minified vs Express ~200KB+ with middleware. Matters for edge cold starts.
First-class TypeScript support with typed context, validators, and route params.
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.