Wednesday, December 25, 2024
Google search engine
HomeLanguagesJavascriptExpress.js app.router Property

Express.js app.router Property

The Express.js app.router property was introduced in Express 4.  It helps us to create modular, mountable route handlers. It provides us with many features such as it extends this routing to handle validation, handle 404 or other errors, etc. It helps us to organize our file structure for our server-side programming.

What is the need to use express.Router?

It helps us to manage hundreds of routes that are created in a project on the server-side by dividing them into individual files. It helps in basic middleware routing and handling 404 errors. Using express.Router, the whole folder containing all the dependencies, files, routes etc is well-structured and is easy to understand for anyone.

Installation of the Express Module

After running npm init and creating a package.json file, it’s time to install our dependency i.e Express.

1. You can visit this link and download using the following command:

npm install express --save

2. After installing express, you can check your express version in the command prompt using the following command:

npm version express

3. After installation of the required dependency, create an app.js file using the terminal. In order to run this file, you need to execute the following:

node app.js

Project Directory: After creating app.js, create a separate folder named routes as shown below:

This will be the project structure after the creation and installation of the files and packages. Inside of routes, there will be two files as shown below:

Filename: app.js

Javascript




// Requiring module
const express = require('express');
  
// Creating express object
const app=express();
  
// Middlewares
app.use(require('./routes/introduction.js'));
app.use(require('./routes/computer.js'));
  
// Server setup
app.listen(3000, function() { 
   console.log('Server listening on port 3000'); 
});


We have required the two files that we have created inside routes i.e computer.js and introduction.js inside our app.js file by using the following code:

// Syntax
app.use(require('Filepath'))

// Implementation
app.use(require('./routes/introduction.js'));
app.use(require('./routes/computer.js'));

Filename: introduction.js

Javascript




// Requiring module
const express = require('express');
  
// Creating router object
const router = express.Router();
  
// Handling request
router.get('/introduction', (req,res) => {
  console.log('Opening introduction.js');
  res.send('Welcome to neveropen!');
});
  
// Exporting router object
module.exports = router;


Filename: computer.js

Javascript




// Requiring module
const express = require('express');
  
// Creating router object
const router = express.Router();
  
// Handling request
router.get('/computer', (req,res) => {
  console.log('Opening computer.js');
  res.send('This is a computer science portal');
});
  
// Exporting router object
module.exports = router;


Run the app.js file using the following command:

node app.js

Output:

Server listening on port 3000

Now open the browser and go to http://localhost:3000/introduction and http://localhost:3000/computer, Then you will see the following output on your terminal screen:

Server listening on port 3000
Opening introduction.js
Opening computer.js

Working: Both the routes have been opened in the browser, so console.log() printed the following statements on successfully opening the routes. On the browser, both the routes will show different outputs as shown below:

For http://localhost:3000/introduction following output will be shown:

Welcome to neveropen!

For http://localhost:3000/computer following output will be shown:

This is a computer science portal

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments