Setting a Default route in Express.js Last Updated : 30 Nov, 2020 Comments Improve Suggest changes Like Article Like Report What is a default route?As we know that in an express application, there are so many URLs and requests involved. It might be possible that the user hit a route which is not included in our express app. In that case, the user will get an error for handling these kinds of issues, we set a default route to our express app that informs the user that, he has hit a wrong route or redirect the user to a specific route. The below is an example error page when a user hits a wrong route: Follow these precautions while defining your default route: There will be only one default route in your web app.The default route will be defined after all other routes for the app are defined i.e. at the end.Steps to set the default route in the express app: Step 1: Create your project folder. Step 2: Now in your terminal, run the following command from the root of your project folder: $ npm init Step 3: Install express using the following command: $ npm install express Step 4: Require 'express' and set your all general routes as per the requirements of your application. Step 5: Below all routes, set your default route as shown below: app.get("*", function (req, res) { res.render("Error_page"); }); Example: An example of an express app with a default route given below. Filename: index.js JavaScript // Requiring modules const express = require("express"); const app = express(); // Root route of express app app.get("/", (req, res) => { res.send("Hello Geeks"); }); app.get("/new", (req, res) => { res.send("welcome to new page"); }); // All the general routes of your // web app are defined above the // default route // Default route app.get("*", (req, res) => { // Here user can also design an // error page and render it res.send("PAGE NOT FOUND"); }); // Server setup app.listen(3001, () => { console.log( `Server listening on http://localhost:3001`); }); Run the index.js file using the following command: node index.js Output: Server listening on http://localhost:3001 Now open your browser and navigate to http://localhost:3001, you will see the following message on screen: Hello Geeks Now hit any different URL other than our defined URL's, like here we have hit http://localhost:3001/xyz. Following will be the output on your screen: PAGE NOT FOUND Comment More infoAdvertise with us Next Article Setting a Default route in Express.js raman111 Follow Improve Article Tags : Technical Scripter Web Technologies Node.js Technical Scripter 2020 Similar Reads Express.js router.all() Function The router.all() function is just like the router.METHOD() methods, except that it matches all HTTP methods (verbs). It is very helpful for mapping global logic for arbitrary matches or specific path prefixes. Syntax:router.all(path, [callback, ...] callback)Parameter: The path parameter is the path 2 min read Express.js | router.use() Function The router.use() function uses the specified middleware function or functions. It basically mounts middleware for the routes which are being served by the specific router. Syntax:router.use( path, function )Parameters:Path: It is the path to this middleware, if we can have /user, now this middleware 2 min read How to create routes using Express and Postman? In this article we are going to implement different HTTP routes using Express JS and Postman. Server side routes are different endpoints of a application that are used to exchange data from client side to server side.Express.js is a framework that works on top of Node.js server to simplify its APIs 3 min read Express.js | app.route() Function The app.route() function returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names (and thus typo errors). Syntax:app.route( path )Installation of the express module:You can visit the link to Install th 2 min read What is Routing in Express? In web applications, without routing it becomes difficult for the developers to handle multiple different requests because they have to manually process each URL request in a single function this problem was solved by the express router which provides a structured way to map different requests to th 5 min read Express.js req.baseUrl Property The req.baseUrl property is the URL path on which a router instance was mounted. The req.baseUrl property is similar to the mount path property of the app object, except app.mountpath returns the matched path pattern(s). Syntax:req.baseUrlParameter: No parameters. Return Value: String Installation o 2 min read Express.js res.set() Function The res.set() function is used to set the response HTTP header field to value. To set multiple fields at once, pass an object as the parameter.Syntax: res.set(field [, value])Parameters: The field parameter is the name of the field and the value parameter is the value assigned to the field parameter 2 min read Express.js router.route() Function The router.route() function returns an instance of a single route that you can then use to handle HTTP verbs with optional middleware. You can also use the router.route() function to avoid duplicate route naming as well as typing errors. Syntax:router.route( path )Parameter: The path parameter holds 2 min read What is the Use of Router in Express.js ? Express.js is a powerful and flexible web application framework for Node.js. It simplifies the process of building server-side applications and APIs by providing robust features and tools. Among these tools, routers play a pivotal role in managing and organizing the routes within an application. Thi 4 min read How to Create a Simple Server Using ExpressJS? The server plays an important role in the development of the web application. It helps in managing API requests and communication between the client and the backend. ExpressJS is the fast and famous framework of the Node.Js which is used for creating the server.In this article, we will create a simp 3 min read Like