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