Typical uses
- Authentication and authorization — verify a token before executing an action
- Rate limiting — restrict requests per IP
- Logging — record request/response details
- CORS — add headers for cross-origin requests
- Response compression — gzip before sending
The chain-of-responsibility pattern
Each middleware implements one method: receive the request, do its job, pass it on. If a condition is not met — return a response and break the chain. This pattern is framework-agnostic and appears identically in PHP, Python (WSGI), Node.js (Express), and Go.