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 to complete the different requests made by the client for e.g. client can make get, put, post, and delete requests these requests can easily handle by these middleware functions. Refer to this article to understand how the Express.js framework works. In this article, we will discuss what is routing and how to create a route using two methods.
Routing: Routing in-network means filtering out the desired path from the bulk of paths available in the computer networks or the server. We can create our own routes with the help of the express.js middleware that routes our path to the desired location.
Working Routing: All incoming requests from the client-side are matched with all the routes available in the project directory. Particular route responds after the matching is successful.
There are two methods to create a route for a particular URL.
- Without using express-generator: In this method, we simply use the express.js module to implement the search URL route. Express.js can handle multiple different requests on the same route.
- With the use of express-generator: express-generator is the quickest way to generate all the necessary files that are required for making Node.js. Express-generator generates default routes as well as default configured app.js file.
Below is the implementation of both methods:
1 Without express-generator: Use a simple express module that handles all the requests from the client as well as checks the incoming requests.
npm install express
Run index.js file using below command:
Output: Visit the http://localhost:3000/ it will redirect to you http://localhost:3000/search/login.
2 Using express-generator: express-generator is the quickest way to generate all the necessary files that are required for making web applications. The advantage of this approach is Express-generator generates default routes as well as default configured app.js file.
npm install -g express-generator
Basic express-generator command:
Generator Files in project structure: After installing the express-generator. We have to create a new file search.js in the routes folder of the express-generator folder.
Run app.js file using below command: