-
Book Overview & Buying
-
Table Of Contents
Full-Stack Web Development with TypeScript 5
By :
Middleware in Hono acts as interceptor functions that process the incoming HTTP request before it reaches the final route handler. Middleware can also process responses before they are sent back to the client. They can modify requests (e.g., parse the body, add headers) and responses (e.g., set cookies, modify headers).
Middleware executes in the order it is defined in the code. Each middleware function can decide whether to pass a request to the next piece of middleware or to end the response cycle. It is commonly used for logging, authentication, error handling, and data parsing.
We are going to implement our own middleware to handle authentication. However, for now, let’s integrate some ready-made middleware provided by Hono. We’ll add one for basic request logging and another to append performance metrics to responses.
Here’s how we modify the beginning of our index.ts file to include these:
src/index.ts
import { Hono } from...