What are middlewares(hoping that this is the plural of middleware)?
In the simplest of words, a middleware function is that function that gets executed between a request coming in and a response going out. We use middlewares to modularize our code, make it readable, increase reusability and much more.
Types of middleware in express
There are 5 types of middleware:
1. Application-level middleware: These middlewares are bound to the app object and called inside the main application file which means that they are executed after every new request.
2. Router-level middleware: These middlewares are bound to a certain router (instance of express.Router) and are then merged with the main application
3. Error-handling middleware: These middlewares accept 4 arguments, of which the first argument refers to the error that is coming in, also to call these middlewares you have to call the next parameter with a parameter like next which actually is the error that has to be handled in the error handling middleware
4. Built-in middleware.: These are middlewares that are provided with expressjs and don't need any third-party packages to be downloaded
5. Third-party middleware.: These middlewares require some third-party packages to be installed first. For example, express session:
If the current middleware function does not end the request-response cycle(redirecting to another request or sending a response), it must call next() to pass control to the next middleware function.
For more in-depth reading visit express middlewares
Top comments (0)