In my last article before this, we looked at the meaning of middleware, its uses or roles and then we ended up by studying the application-level middleware. In this new tutorial, we will look at the following about the router-level middleware;
- Definition
- Use and
- Example
Now let’s try explaining what the router-level middleware is all about. Remember the application level middleware is bound to an instance of the app object which could either be with app.use() or app.METHOD () where the method refers to the http method (get, put, post, delete).
The router-level middleware works like the application-level middleware except for the fact that it is bound to a router instance express.router (). It can be loaded with router.use or router.METHOD
As the name implies, this is a middleware used in creating routes. Though there’s another alternative to creating a route in a node js app, there are cases where a middleware for a particular functionality is very necessary especially medium-large apps. Also, the router middleware is used to group the routes of a website together and access them easily.
Just like application middleware, router-level middleware also takes the parameter next which is used to pass on to the next middleware in our express app. You can read more about this on express js website ().
Example
In this example, we will setup a simple express app with a route using the router middleware.
server.js
Thanks for staying here. You can always ask your questions or give us your feedback.
Bye!✔
Comments
Post a Comment
Please do not enter any spam link in the comment box.