Routing and configuration

Reuse Hono routes

Bring the web-standard routing and validation patterns from the Web APIs course into a Cloudflare Worker entry point.

8 minute lesson

~~~

Hono runs natively on Workers because its core uses Request and Response rather than Node-specific server primitives.

Create a Hono app, define /api/links routes, and export the app. Keep platform-independent validation and problem-response helpers in ordinary modules; use bindings only in repository adapters.

import { Hono } from 'hono'

const app = new Hono<{ Bindings: Env }>()
app.get('/api/health', c => c.json({ ok: true }))
export default app

Hono should own HTTP concerns: route matching, middleware, validation errors, and response formatting. Repository adapters should own D1 or KV operations. This boundary lets pure validation tests run without Cloudflare resources while runtime tests exercise the real binding adapters.

Middleware order is observable behavior. Put request IDs and error handling around routes, authenticate before protected handlers, and ensure the final 404 still returns the API’s JSON error shape. Avoid catching every exception inside a route and returning 200; preserve meaningful status codes and log the internal error once with safe context.

Add collection and detail route placeholders with the same statuses and errors used in the Books API.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →