How to Create Multiple Routes in the Same Express.js Server ? Last Updated : 20 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Creating multiple routes in an Express.js server involves defining routes in separate modules. This modular approach allows for better organization and maintainability by managing different routes independently and mounting them in the main server file. ApproachTo create multiple routes in an Express.js server, use express.Router() to define routes in separate modules. Import and mount these route modules in your main server file using app.use(). This modular approach enhances organization and maintainability. Express RouterThe express.Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express.js.This is the advantage of the use of the Router. Syntax: express.Router( [options] )Optional Parameters: Case-sensitive: This enables case sensitivity.mergeParams: It preserves the req. params values from the parent router.strict: This enables strict routing.Return Value: This function returns the New Router Object. Structure Model :Installing Module: npm install expressProject structure: Example: This example demonstrates creating multiple routes in sercer using express router Node // Home.js // Importing express module const express=require("express") const router=express.Router() // Handling request using router router.get("/",(req,res,next)=>{ res.send("This is the homepage request") }) // Importing the router module.exports=router Node // login.js // Importing the module const express=require("express") // Creating express Router const router=express.Router() // Handling login request router.get("/",(req,res,next)=>{ res.send("This is the login request") }) module.exports=router Node // index.js const express=require("express") // Importing all the routes const homeroute=require("./routes/Home.js") const loginroute=require("./routes/login") // Creating express server const app=express() // Handling routes request app.use("/home",homeroute) app.use("/login",loginroute) app.listen((3000),()=>{ console.log("Server is Running") }) Run index.js using the below command: node index.jsOutput: Handling the /home request by the client. Comment More infoAdvertise with us Next Article How to Create Multiple Routes in the Same Express.js Server ? Z zack_aayush Follow Improve Article Tags : Web Technologies Node.js NodeJS-Questions Similar Reads How to Configure multiple View Engines in Express.js ? View engines present in web application framework are basically the template engines that allow us to embed dynamic content into the web pages, render them on the server, and send them to the client. With the help of these, we can serve dynamic data, and utilise the template inheritance properties t 3 min read How to skip a middleware in Express.js ? If we want to skip a middleware we can pass parameters to the middleware function and decide based on that parameter which middleware to call and which middleware to not call. Prerequisite: express.js: To handle routing. Setting up environment and execution: Step 1: Initialize node project npm init 1 min read How to create custom middleware in express ? Express.js is the most powerful framework of the node.js. Express.js is a routing and Middleware framework for handling the different routing of the webpage, and it works between the request and response cycle. Express.js use different kinds of middleware functions in order to complete the different 2 min read How to set Multiple Web Apps with Different Base Directories on the Same Express Server ? Node.js supports server side functionality using Express. It allows us to add new features and organize the functionalities. While working with Express, we can set up our own local server for a given base directory. The following approach covers how to serve multiple web apps with different base dir 2 min read How to Structure my Application in Express.js ? A clean and well-organized folder structure is crucial for building maintainable and scalable Express.js applications. This article explores best practices for structuring your application, organizing components, and maintaining a modular architecture.Why Structure an Express Application?A well-stru 6 min read Like