Express.js app.router Property
Last Updated :
30 Nov, 2020
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 geeksforgeeks!');
});
// 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 geeksforgeeks!
For http://localhost:3000/computer following output will be shown:
This is a computer science portal
Similar Reads
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read
HTML Tutorial
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
JS Tutorial
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.Client Side: On the client side, JavaScript works
11 min read
JSON TutorialJSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. Used Extensively : Used in APIs, configuration files, and data exchange between servers and clients.Text-based: JSON is a simple text format, making it lightweight and easy to transmit.Human
5 min read
TypeScript TutorialTypeScript is a superset of JavaScript that adds extra features like static typing, interfaces, enums, and more. Essentially, TypeScript is JavaScript with additional syntax for defining types, making it a powerful tool for building scalable and maintainable applications.Static typing allows you to
8 min read
Vue.js TutorialVue.js is a progressive JavaScript framework for building user interfaces. It stands out for its simplicity, seamless integration with other libraries, and reactive data binding.Built on JavaScript for flexible and component-based development.Supports declarative rendering, reactivity, and two-way d
4 min read
jQuery TutorialjQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
Front End
React TutorialReact is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
Angular TutorialAngular is a powerful, open-source web application framework for building dynamic and scalable single-page applications (SPAs). Developed by Google, Angular provides a comprehensive solution for front-end development with tools for routing, form handling, HTTP services, and more.Designed for buildin
4 min read
Backend
Node.js TutorialNode.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme
4 min read
Express.js TutorialExpress.js is a minimal and flexible Node.js web application framework that provides a list of features for building web and mobile applications easily. It simplifies the development of server-side applications by offering an easy-to-use API for routing, middleware, and HTTP utilities.Built on Node.
4 min read
PHP TutorialPHP is a popular, open-source scripting language mainly used in web development. It runs on the server side and generates dynamic content that is displayed on a web application. PHP is easy to embed in HTML, and it allows developers to create interactive web pages and handle tasks like database mana
9 min read
Laravel TutorialLaravel is an open-source PHP web application framework that has gained immense popularity since its inception in 2011, created by Taylor Otwell. This renowned framework empowers developers to build robust, scalable web applications with remarkable ease. As a developer-friendly framework, Laravel of
3 min read
Database
Web Technologies Questions The following Web Technologies Questions section contains a wide collection of web-based questions. These questions are categorized based on the topics HTML, CSS, JavaScript, and many more. Each section contains a bulk of questions with multiple solutions. Table of Content HTML QuestionsCSS Question
15+ min read