What is middleware in a web framework?
Options
- A database driver that connects the app to the database engine
- Code that wraps each request and response, like logging or auth (Correct option)
- A frontend library that is used for building user interfaces
- A message broker that delivers events between the services
Answer
Code that wraps every request/response, e.g. logging, auth, CORS, timing
Why
Middleware is code that runs before and/or after EVERY request, around your endpoint functions, like layers of an onion.
Typical uses: logging, measuring response time, checking authentication, adding CORS headers, handling errors.
The order matters: e.g. authentication must run before your route logic.
Remember
Middleware = code that runs around every request.