Middleware is an entity that hooks into a server's request/response life cycle. The middleware can be defined in many components. Each component has a specific function to perform. Whenever we define handlers for URL patterns (as in Chapter 2, Handling Routing for our REST Services), a handler executes some business logic for every incoming request. But middleware, as the name specifies, sits between a request and the handler, or between a handler and a response. So, virtually every middleware can perform these functions:
- Process the request before reaching the handler (function)
- Pass the modified request to the handler function (execute some business logic)
- Process the response coming from the handler
- Pass the modified response to the client
We can see the previous points in the form of a visual illustration, as shown in the following diagram:
If...