How to create multiple routes in the same express.js server ?
Express.js is a powerful framework for node.js. One of the main advantages of this framework is defining different routes or middleware to handle the client’s different incoming requests. Express. js allows us to create multiple routes on a single express server. Creating multiple routes on a single server is better to practice rather than creating single routes for handling different requests made by the client. In this article, we will discuss how to create multiple routes on a single express server.
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. 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.
express.Router( [options] )
- 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 :
npm install express
Run index.js using the below command:
Output: Handling the /home request by the client.
Please Login to comment...