Hono foundations

Return clear responses

Use text, JSON, redirects, bodies, headers, and status codes as one intentional response contract.

9 minute lesson

~~~

A created bookmark returns JSON, a creation status, and a location header, while deletion returns no invented payload. This is a small part of a typed bookmarks API built with Hono, tested in memory, and deployed once to Node and once to Cloudflare Workers, but the boundary it creates affects everything that follows.

Hono helpers create responses; they do not decide which representation or status is correct for the API. The useful target is not encyclopedic coverage. Make one deliberate choice, observe its consequences, and know which requirement would make you revise it.

Watch for one shortcut in particular: trying to return successful JSON for every branch because the helper is convenient. It makes later failures harder to locate. Instead, define route outcomes first, then use the helper that expresses the required status, headers, and body. Prefer an implementation you can explain from the outside before optimizing it.

Keep the wider goal in view: Build the smallest portable Hono application and identify exactly which parts depend on its runtime. A short observation with concrete evidence is more useful than a confident sentence with no reproduction path.

Create a response table for success, invalid input, missing bookmark, conflict, and unexpected failure. Hand the result to someone else and see whether the behavior is clear without an oral explanation.

Lesson completed

Take this course offline

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

Get the download library →