What happens on every request.
THE SEQUENCE
A request arrives at the server It is routed to a handler
Middleware runs: logging, authentication, parsing
The handler executes business logic Data is read or written A response is built and returned
WHAT MIDDLEWARE IS
Code running before or after a handler, applied across many routes.
WHAT IT TYPICALLY DOES
Parses the body Establishes who is requesting Logs Handles errors Applies rate limits Sets headers
WHY IT MATTERS
It prevents repeating the same concerns in every handler.
WHAT ORDER MATTERS FOR
Authentication before authorisation Parsing before anything reading the body Error handling registered so it catches everything
WHAT A HANDLER SHOULD DO
One thing, briefly.
WHAT IT SHOULD NOT CONTAIN
Business logic of any length.
WHAT TO DO INSTEAD
Delegate to a service or a function, and keep the handler concerned with the request and response.
WHY
Logic inside handlers cannot be reused or tested independently.
WHAT TO RETURN ALWAYS
An appropriate status code and a consistent body shape.