Basics
Everything a Hono app is made of, on one growing cats API: routes and their handlers, the context that reads a request and writes a response, middleware around the handler, validation, error handling, cookies, streaming, composition and tests. One lesson per page of the official docs' API and Guides chapters.
- lessons
- 10
- level
- beginner
- time
- 4 hours
Routes and handlers
A Hono app is a list of routes, each a path and a handler that turns a context into a Response.
- 1First steps
Create a Hono app, answer two routes with text and with JSON, and export it so the runtime can call its fetch(), the way Cloudflare Workers, Bun and Deno do.
Read the theory - 2Routing
Give the cats API its routes: a parameter in the path, two parameters, a route that must be registered before the parameterised one, and a DELETE that answers 204.
Read the theory - 3The context
Read what a request carries, its query string, its headers and its JSON body, and shape the response: a status, a header, text or JSON depending on what the client accepts.
Read the theory
Around the handler
What runs before and after a handler, and what happens when a request is wrong or a handler throws.
- 4Middleware
Run code around every handler: a middleware that tags each request with an id the handler can read, and one that guards the cats routes with an API key and answers 401 itself.
Read the theory - 5Validation
Check a JSON body, a path parameter and a query value before the handler runs, with Hono's validator, and answer 400 with a message that names the problem.
Read the theory - 6Error handling
Throw HTTPException where a problem is found, and give the whole API one shape for errors with app.onError() and app.notFound(), including what a crash looks like from outside.
Read the theory
Growing the app
State across requests, responses that do not end at once, an app split into files, and tests that call it without a server.
- 7Cookies
Keep state across requests the way a browser does: a session cookie set at login and read on every request, a preference cookie that outlives the session, and a logout that deletes one without touching the other.
Read the theory - 8Composing apps
Split the API into files, one Hono app per resource, mount them with app.route() and put the whole thing under a base path.
Read the theory - 9Streaming
Answer before the whole body exists: a text export written line by line with streamText(), and a feed of server-sent events with streamSSE() that keeps the connection open.
Read the theory - 10Testing
Test the cats API without a server: build a fresh app per test, call it with app.request(), and prove the tests mean something by catching four bugs planted in the code.
Read the theory