The Wayback Machine - https://web.archive.org/web/20240124090513/https://www.geeksforgeeks.org/express-js-router-param-function/
Open In App
Related Articles

Express.js router.param() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Syntax:

router.param(name, function)

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.

javascript




//
const express = require("express");
const app = express();
 
//import router module from route.js file
const userRoutes = require("./route");
 
app.use("/", userRoutes);
 
//PORT
const port = process.env.PORT || 8000;
 
//Starting a server
app.listen(port, () => {
  console.log(`app is running at ${port}`);
});


We have to create another file named route.js in the same directory

Code for route.js file

javascript




const express = require("express");
const router = express.Router();
 
router.param("userId", (req, res, next, id) => {
    console.log("This function will be called first");
    next();
});
 
router.get("/user/:userId", (req, res) => {
    console.log("Then this function will be called");
    res.end();
});
// Export router
module.exports = router;


Steps to run the program:

Start the server by entering the following command

node app.js

Output:

Enter the following address into the browser

http://localhost:8000/user/343

You will see the following output in your terminal


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 20 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials