Using the middleware pattern
The heart of Express is the middleware pattern, which allows you to extend the functionality of the framework by adding functions that will be executed in the request-response cycle. The middleware functions are executed in the order that they are added to the application, and they can be added to the application or alternatively to a route.
Figure 10.5 – Middleware pattern full process from application middleware to main function
We can understand the middleware pattern as a pipeline, where the request is passed through the pipeline, and each middleware function can modify the request and the response, and pass the request to the next middleware function in the pipeline. The middleware functions can also end the request-response cycle by sending a response to the client.
Figure 10.6 – Middleware pattern limited to Application Middleware
We can add a global middleware to the application...