Express.js | app.param() Function - GeeksforGeeks

18 Jun, 2020
The app.param() function is used to add the callback triggers to route parameters. It is commonly used to check for the existence of the data requested related to the route parameter.
Syntax:
app.param([name], callback)
Parameters:
name: It is the name of the parameter or an array of them.callback: It is a function that is passed as a parameter.
name: It is the name of the parameter or an array of them.
callback: It is a function that is passed as a parameter.
Installation of express module:
You can visit the link to Install express module. You can install this package by using this command.npm install expressAfter installing the express module, you can check your express version in command prompt using the command.npm version expressAfter that, you can just create a folder and add a file for example, index.js. To run this file you need to run the following command.node index.js
You can visit the link to Install express module. You can install this package by using this command.npm install express
npm install express
After installing the express module, you can check your express version in command prompt using the command.npm version express
npm version express
After that, you can just create a folder and add a file for example, index.js. To run this file you need to run the following command.node index.js
node index.js
Filename: index.js
var express = require('express');var app = express();var PORT = 3000;  app.param('id', function (req, res, next, id) {    console.log('CALLED ONLY ONCE');    next();});    app.get('/user/:id', function (req, res, next) {    console.log('Greetings from geeksforgeeks');    next();});    app.get('/user/:id', function (req, res) {    console.log('Once again greetings from geeksforgeeks');    res.end();});  app.listen(PORT, function(err){    if (err) console.log("Error in server setup");    console.log("Server listening on Port", PORT);});
Steps to run the program:
The project structure will look like this:Make sure you have installed express module using the following command:npm install expressRun index.js file using below command:node index.jsOutput:Server listening on Port 3000
Open browser and go to http://localhost:3000/user/42. Now check your console, following will be the output:Server listening on Port 3000
CALLED ONLY ONCE
Greetings from geeksforgeeks
Once again greetings from geeksforgeeks

The project structure will look like this:
Make sure you have installed express module using the following command:npm install express
npm install express
Run index.js file using below command:node index.jsOutput:Server listening on Port 3000

node index.js
Output:
Server listening on Port 3000

Open browser and go to http://localhost:3000/user/42. Now check your console, following will be the output:Server listening on Port 3000
CALLED ONLY ONCE
Greetings from geeksforgeeks
Once again greetings from geeksforgeeks

Server listening on Port 3000
CALLED ONLY ONCE
Greetings from geeksforgeeks
Once again greetings from geeksforgeeks

So this is how you can use the express app.param() function to add callback triggers to route parameters.
Express.js
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to read and write Excel file in Node.js ?
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript