Node Js Router module is used to create endpoints for applications for
handling client requests. A route is a chunk of Express code that links an
HTTP verb (GET, POST, PUT, DELETE, etc.) to a URL path/pattern and a function
that handles that pattern.
Routes may be created in a variety of ways. We’ll be using the npm module
express in this lesson. Router middleware is useful since it allows us to
aggregate route handlers for a certain section of a website and accesses them
using a single route prefix. We’ll store all of our library-related routes in
a “catalogue” module, and we’ll maintain them separated if we add routes for
managing user accounts or other tasks.
Node Js Router module
In a module called Login.js, we first construct Login routes. The code imports
the Express application object uses it to acquire a Router object, and then
uses the get() function to add a handful of routes to it. Finally, the Router
object is exported by the module.
var express = require('express');
var router = express.Router();
// Login page.
router.get('/', function (req, res) {
res.send('Login page');
})
// Register Page
router.get('/about', function (req, res) {
res.send('You can view Dashboard');
})
module.exports = router;
We call an instance of express.router(). we use those routes to view the Login
page as well as Dashboard. we can access the Login page at
https://localhost:3001/ and Dashboard at
https://localhost:3001/about,
var Login = require('./Login.js');
// ...
app.use('/Login', Login);
In order to utilize the router module in our main app code, we must first require() it (Login.js). The Router is then added to the middleware processing path by using use() on the Express application with a URL path of ‘Login’.
Now we set a middleware as Login.js. Now our URL has been changed https://localhost:3001/Login/ and https://localhost:3001/Login/about.
We can create such routes more and then apply them to our application, this is a very powerful feature. A Router could be used for basic routes, authenticated routes, and API routes.
By generating numerous instances of the Router and applying them to our apps, we can make them more modular and versatile than ever before. We’ll now look at how to process requests with middleware.
Route Paths
The endpoints at which requests can be made are defined by the route routes. ‘/’, ‘/about’, ‘/book’, and ‘/any-random.path’ is the only strings we’ve seen so far, and they’re utilized precisely as stated.
String patterns can also be used as route routes. To create patterns of endpoints that will be matched, string patterns utilize a type of regular expression syntax. The syntax is as follows (note that string-based paths interpret the hyphen (-) and the dot (.) literally):
+: The endpoint must contain one or more of the previous characters (or groups), for example, a route path of ‘/ab+cd’ will match abcd, abbcd, abbbcd, and so on.
(): ‘/ab(cd)?e’ performs a?-match on the group (cd)—it matches abe and abcde.
?: The previous character (or group) must be 0 or 1, e.g., a route path of ‘/ab?cd’ will match endpoints acd or abcd
*: The * character may be placed anywhere in the endpoint’s string. Endpoints abcd, abXcd, abSOMErandomTEXTcd, and so on will be matched by a route path of ‘/ab*cd’, for example.
Conclusion
We now have more flexibility than ever before in creating our routes thanks to the integration of the Express 4.0 Router. To summarise, we can:
- To define groups of routes, call express.Router() multiple times.
- Use app.use() to apply express.Router() to a portion of our site and handle requests with route middleware.
- To validate parameters using .param, use route middleware ()
- To define numerous requests on a route, use app.route() as a shortcut to the Router.
With all of the different methods, we can create routes, I’m confident that our applications will improve in the future. If you have any questions or recommendations, please leave them in the comments section.
Comments
Post a Comment