Skip to content

Building with Hono

Time to assemble the pieces. A clean implementation separates routing, validation, and business logic, and wires cross-cutting concerns (auth, errors, CORS) as middleware.

src/
index.ts // create the app, mount middleware + routes, start the server
middleware/ // auth, error handler, CORS, rate limiting
routes/ // one module per resource (articles, users, ...)
services/ // business logic, independent of HTTP
schemas/ // request/response validation (e.g. zod)

Handlers stay thin: validate input, call a service, shape the response. Services know nothing about HTTP, so they are easy to unit-test.

This Hono app brings together validation, a consistent error shape, and proper status codes for an articles resource. Open it in StackBlitz to run it:

JavaScript

Notice how every idea from the course shows up: a paginated collection envelope, 201 Created with Location, 422 validation with field errors, 404 via a thrown typed error, and one central onError producing problem+json.

Why keep services free of HTTP concerns?
What belongs in a thin handler?
Where is the single consistent error shape produced in this app?