Routing and middleware

Understand middleware order

Trace before-next and after-next behavior through nested middleware and handlers.

9 minute lesson

~~~

Start from the behavior you can observe. A timer starts before authentication and records the final status after the route returns.

Hono middleware composes like an onion: code before await next() runs inward and code after it runs outward. The useful target is not encyclopedic coverage. Make one deliberate choice, observe its consequences, and know which requirement would make you revise it.

It is tempting to call next without awaiting it or assume after-code runs before the handler. That trades a clear decision for an assumption you will eventually have to debug. The stronger move is to diagram the entry and exit order and await next whenever later response state matters. Make the boundary explicit in code and in the project notes.

Tie the decision back to the larger job: Compose routes and middleware so the request path stays short, typed, and visible. Record enough evidence that another developer can repeat the result without relying on your memory.

Add three named middleware layers, predict all log lines, and compare the actual order for success and failure. Repeat it once with an invalid or hostile condition and write down the boundary the failure revealed.

Lesson completed

Take this course offline

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

Get the download library →