How do you handle nested routes in Express.js? Last Updated : 24 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article we are going to learn that how can we setup nested routes in Express JS. Nested routes are the routes that are defined within other routes. This is used to organize your code.We are going to implement the nested routes using the below given two approaches.Table of ContentUsing Express JSUsing Express JS RouterUsing Express JSWe are going to create nested routes by using Express JS only, in which we will create nested routes with HTTP methods only, and we do not need to use any other module in it.Syntax:app.get('base URL',()=>{})app.get('base URL/Nested URL',()=>{})Example: Implementation of above approach. JavaScript // Server.js const express = require('express'); const app = express(); const PORT = 3000; app.use(express.json()); // Parent Route app.get("/", (req, res) => { console.log("GET Request Successfull!"); res.send("Get Req Successfully initiated"); }) // Nested Route app.get("/user", (req, res) => { res.send(`Client is requesting for USER Data`) }) // Nested Route app.get("/user/:id", (req, res) => { res.send(`Client required ID -> ${req.params.id}`) }) app.listen(PORT, () => { console.log(`Server established at ${PORT}`); }) Output:Using Express JS RouterThrough the Express JS Router, we will create a base URL, and then we can directly use the router to implement a nested URL through it. The 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. Syntax:express.Router( [options] )Example: This example implements nested routing using Express JS Router JavaScript // Server.js const express = require('express'); const app = express(); const PORT = 3000; const router = express.Router(); // Nested Route router.get('/', (req, res) => { res.send("USER NESTED API IS Endpoint!"); }) // Another Nested Route router.get('/api', (req, res) => { res.send("/API endpoint") }) // Parent Route app.use('/user', router); app.listen(PORT, () => { console.log(`Server established at ${PORT}`); }) Output: Comment More infoAdvertise with us Next Article How do you handle nested routes in Express.js? P prathamgfg Follow Improve Article Tags : Web Technologies Node.js Express.js Similar Reads How to handle redirects in Express JS? Express JS uses redirects to send users from one URL to another. This can be done for various reasons, such as handling outdated URLs or guiding users through a process. In ExpressJS, you define routes and use them to direct users to the desired URL. It's a way to keep your application organized and 2 min read How To Handle Route Parameters in Express? Route parameters in ExpressJS capture dynamic values from URLs, like /users/:userId. These values are accessible in your route handler via req.params, enabling dynamic content generation. This allows for creating reusable routes that handle various inputs with a single pattern.JavaScriptapp.get('/us 4 min read How do you access query parameters in an Express JS route handler? Express JS is a popular web framework for Node JS, simplifies the development of web applications. One common task is accessing query parameters from URLs within route handlers. In this article, we'll explore the fundamentals of working with query parameters in Express JS and demonstrate how to acce 2 min read How to handle sessions in Express ? ExpressJS is a small framework that works on top of Node web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. It adds helpful utilities to Node HTTP objects and facilitates the render 4 min read How to Use Handle Get Request in Express.js ? Express.js is a popular web application framework for Node.js, known for its simplicity and flexibility. One of the fundamental tasks in web development is handling HTTP requests, and GET requests are among the most common. This article will guide you through the process of handling GET requests in 2 min read Like