Middleware is a JavaScript pattern, similar to the Chain of Responsibility (CoR) pattern, where a chain of functions is used to handle an incoming event, often in the form of a request.
This pattern is especially useful in libraries that deal with events (such as requests in express or actions in Redux). The middleware pattern has several benefits:
- It allows us to do the following:
- Use several functions to handle the event and process the data
- Have functions act as a filter for certain kinds of data
- It doesn't require explicit knowledge between sender and receiver
- The chain of functions can be changed during runtime, acting as an event pipeline
Certain libraries, such as express, allow you to extend their functionality using middleware. This is usually done using .use() on the instance (in express, the instance will be the app object). We pass a middleware...