Express.js app.router() Method
Express.js is a Node.js web flexible framework that provides a set of features for mobile and web applications. Express has various methods provided by many developers and one of the methods in Express is a Router which is used to divert the user to different pages of the website base on request.
Syntax :
express.Router([options]);
Parameter: This function accepts the following parameters:
- case sensitive: It enables the case sensitivity means if the route is ‘/contact’ it does not mean that it is the same as ‘/Contact’, ‘/contact’ etc by default it Ignores the case sensitivity.
- mergeParams: This feature is available on express version 4.5.0 and above. You need to pass mergeParams as true if you want to access params from the Parent route via the child route.
- strict: it enables strict Routing it means if the route is ‘/about’ it does not mean that it is the same as ‘/about/’ by default it is the opposite.
Project Setup and Module Installation:
Step 1: You can visit the link to Install express module. You can install this package by using this command.
npm install express
Step 2: After installing the express module, you can check your express version in the command prompt using the command.
npm version express
Step 3: After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
node index.js
Project Structure: It will look like the following.
Filename- index.js:
Javascript
// Requiring module const express = require( 'express' ); const app = express(); // Port number const port = process.env.PORT || 4000; // import router which is exported // in app.js file const route = require( './routes/app.js' ); // When a request comes from /result // route.It divert to app.js app.use( '/result' , route); const visit_link = "<a" + " href=" + "/result" + 'style="color:green;"' + '"text-decoration:none;"' + '"text-size:20px" + ">"' + "Hello Beginner" + "</a>" + "<br> <br> Click Hello Beginner" ; // Handling GET Request '/' app.get( '/' , function (req, res) { // Sending the html code as a string res.send(visit_link); }); // Server setup app.listen(port, function (req, res) { console.log( "listen" ); }); |
Router() Method in Express: When your web or mobile application has many routes, a developer can’t maintain the code readability, cleanliness, consistency, and correctness by maintaining all the routes in one file. So express developer comes with a greater idea and introduces a method called Router which helps the developer to maintain all the requirements.
In this example project, when the user clicks on the Hello Beginner, it is redirected to http://localhost:4000/result. When the request contains the ‘/result’ route then the server runs the app.js file which is inside the routes folder.
Filename app.js
Javascript
// Requiring module const app = require( 'express' ) // Initiate router const router = app.Router(); // Path Module const path = require( 'path' ); // Handling GET Request router.get( '/' , function (req,res) { res.sendFile(path.dirname(__dirname) + "/index.html" ) }) // Exporting router variable module.exports = router; |
Filename- index.html: After the complete execution of the app.js file, it sends an HTML file to that particular route and shows it on the browser.
HTML
<!DOCTYPE html> < html > < body > < img style="margin-left:auto; margin-right:auto; display: block;width: 50%;" src = "https://media.w3wiki.net/wp-content/uploads/w3wiki-15.png" alt = "w3wiki" width = "500" height = "250" > </ body > </ html > |
Step to run the Application: Run the index.js file by using the following command:
node index.js
Output: Now open the browser and go to http://localhost:4000/, you will get the following output.
Reference: https://expressjs.com/en/5x/api.html#express.router
Contact Us