Middleware — Basics · Hono

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.

What you will learn

Read the theory for Middleware

All Basics lessons

All Hono courses

loading types…

What you'll learn

  • Write middleware with createMiddleware(): code before await next() runs before the handler, code after it runs after
  • Pass a value from middleware to the handler with c.set() and c.get(), typed through the app's Variables
  • Scope middleware to a path with app.use('/cats/*', …) and end a request early by returning a response