<h1><center>Introduction to routing in Express </center></h1> ## Abstract Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on). Each route can have one or more handler functions, which are executed when the route is matched. ## Scope - In this article we will be focusing on a simple routing example for express.js - A little intro to Routing - The given example can be run over vscode - This article focuses on routes for a module with crud operations ## Introduction A Route is a section of Express code that associates an HTTP Request, a URL path/pattern, and a function that is called to handle that pattern. You define routing using methods of the Express **app** object that correspond to HTTP methods; for example, **app.get()** to handle GET requests and **app.post** to handle POST requests. For a full list, see [**app.METHOD**](https://expressjs.com/en/4x/api.html#app.METHOD). You can also use [**app.all()**](https://expressjs.com/en/4x/api.html#app.all) to handle all [HTTP methods](https://en.wikipedia.org/wiki/HTTP#Request_methods) and [**app.use()**](https://expressjs.com/en/4x/api.html#app.use) to specify [middleware](https://expressjs.com/en/guide/using-middleware.html) as the callback function (See Using middleware for details). These routing methods specify a callback function (sometimes called “handler functions”) called when the application receives a request to the specified route (endpoint) and HTTP method. In other words, the application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function. In fact, the routing methods can have more than one callback function as arguments. With multiple callback functions, it is important to provide next as an argument to the callback function and then call **next()** within the body of the function to hand off control to the next callback. - Using app.method we can easily configure routes in our application - we can configure one or more routes in the app - we can configure routes with any HTTP methods ## Defination Route definition takes the following structure: ``` app.METHOD(PATH, HANDLER) ``` Where: - app is an instance of express. - METHOD is an [HTTP request method](https://en.wikipedia.org/wiki/HTTP#Request_methods), in lowercase. - PATH is a path on the server. - HANDLER is the function executed when the route is matched. ## Prerequisites - node - nodemon ``` $ npm install -g nodemon ``` - express ``` $ npm install express ``` ## Example Some Examples of routes : - app.post('/create') - app.get('/users') - app.put('/user/update') - app/delete('/user/delete') ## Code Example To create an instance of express app follow these : ``` var express = require("express"); var app = express(); ``` Lets write a simple request ![](https://i.imgur.com/7MaMsY0.png) To Test this out we will go to postman after running the application To run the file just run ``` node [filename] ``` in the VSC terminal ![](https://i.imgur.com/XlsPoDm.png) In postman type your localHost URL to test or just type the URL on your browser ![](https://i.imgur.com/cacnhIl.png) Let's Define few user objects ``` var express = require("express"); var app = express(); app.get('/read-all-users',(req,res)=>{ res.send("List of Users"); }); app.get('/get-user-details',(req,res)=>{ const userObj ={ id : 10, name : "ABC", lastname : "EFG", status : true } res.send(userObj); }); app.listen(4001); ``` If we route to read-all-users we should get the message "List of Users" ![](https://i.imgur.com/7KR2jy8.png) If we route to get-user-details we should get the user object as the output ![](https://i.imgur.com/FXwe63x.png) Lets say we have multiple modules to route to say for example in a E-commerce site we need : - Users - Products - Items - Reviews - Comments - Images - Videos - Returns - Orders All if this will have crud operations. Lets add Crud operations in our app. If we have to add we need to add to all modules and it will look too messy. ![](https://i.imgur.com/R7HsKai.png) To over come this we do a modular programming we use router - over the period of time routes grow in size and is extermely difficult to manage - one of the beauty of ExpressJS Framework is how easy it is to manage the code - Using modular approach using Router we can easily develop, maintain and extend routes - we will need to get the Router object and then create routes for the modules we create new js files : ![](https://i.imgur.com/eGXCpVx.png) Lets write some get requests in products.js ![](https://i.imgur.com/Fevk2du.png) To import this in index.js we do : ![](https://i.imgur.com/wa9a3Fg.png) Test the routes in your Postman tool ![](https://i.imgur.com/XWE3DKF.png) Write get requests for Users.js as well ![](https://i.imgur.com/altpKHF.png) also shift all the crud operations in index.js to users.js ![](https://i.imgur.com/1CUeGQe.png) Do the same to products.js aswell ![](https://i.imgur.com/kPTQSVF.png) Now your index.js is reduced to only this short modularized snippet :D : ![](https://i.imgur.com/4a2rwS9.png) This is much easier to work with and is clean and modular. Testing the get requests using postman tool Post request from Postman tool : ![](https://i.imgur.com/gNK8ETR.png) Put Request from Postman tool : ![](https://i.imgur.com/yAimOAS.png) ## Conclusion Yay! ✨ Your finally done with your first routing application. Using basic routes using ExpressJs. You can add as many modules as you like and router can be used to route between the routes. The final code is modularized clean and efficient very easy to work on. Debugging the code will be a lot easier when the code is modularized and routing helps in modularizing the code. Implemented a basic E-commerece like page routing using Express.js, we can go a step further by adding some dynamic routing too which needs another blog on its own :)