Express.js router.param() function
The parameters of router.param() are a name and function. Where the name is the actual name of the parameter and the function is the callback function. Basically, the router.param() function triggers the callback function whenever the user routes to the parameter. This callback function will be called only a single time in the request-response cycle, even if the user routes to the parameter multiple times.
Parameters of the callback function are:
- req: the request object
- res: the response object
- next: the next middleware function
- id: the value of the name parameter
First, you need to install the express node module into your node js application.
Installations of express js are as follows:
npm init npm install express
Example: Create a file names app.js and paste the following code into the file.
We have to create another file named route.js in the same directory
Code for route.js file
Steps to run the program:
Start the server by entering the following command
Enter the following address into the browser
You will see the following output in your terminal
Please Login to comment...