Middleware
What is it?
Middleware is software that sits between incoming requests and your route handlers in a web application, processing requests before they reach your business logic and optionally modifying responses before they are sent to clients. Common middleware handles authentication, logging, error handling, request body parsing, compression, rate limiting, and adding security headers to responses.
Practical example
In an Express.js application, middleware functions are chained with app.use. A typical setup includes cors middleware for cross-origin requests, express.json for parsing JSON request bodies, a custom authMiddleware that verifies JWT tokens and attaches user info to the request, and an errorHandler middleware at the end that catches any errors and sends formatted error responses. Each request flows through this chain in order.
Test your knowledge
What is a common use of middleware?