Express.js app.router() Method

02 Nov, 2021
Express.js is a Node.js web flexible framework that provides a set of features for mobile and web applications. Express has various methods provide by many developers and one of the methods in the express is 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:
caseSensitive:  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.
Project Structure
Filename- index.js:
Javascript
// Requiring moduleconst express = require('express');const app = express(); // Port numberconst port = process.env.PORT || 4000; // import router which is exported// in app.js fileconst route = require('./routes/app.js'); // When a request comes from /result// route.It divert to app.jsapp.use('/result', route); var visit_link = "<a" + " href=" + "/result"    + 'style="color:green;"'    + '"text-decoration:none;"'    + '"text-size:20px" + ">"'    + "Hello Geeks" + "</a>"    + "<br> <br> Click Hello Geeks"; // Handling GET Request '/'app.get('/', function(req, res) {     // Sending the html code as a string    res.send(visit_link);}); // Server setupapp.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 introduce a method called Router which help developer to maintain all the requirements.
In this example project, when the user clicks on the Hello Geeks, 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 moduleconst app = require('express') // Initiate routerconst router = app.Router(); // Path Moduleconst path = require('path'); // Handling GET Requestrouter.get('/',function(req,res) {  res.sendFile(path.dirname(__dirname)  + "/index.html")}) // Exporting router variablemodule.exports = router;
 
 
Filename- index.html:
 
After 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.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png"        alt="geeksforgeeks" 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.
Working model of this project
 
Reference: https://expressjs.com/en/5x/api.html#express.router
sumitgumber28
Express.js
Picked
Technical Scripter 2020
Node.js
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JWT Authentication with Node.js
Installation of Node.js on Windows
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Express.js req.params Property
How to connect Node.js with React.js ?
Mongoose | updateOne() Function
File uploading in Node.js
Node.js Export Module