Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Express.js – app.param() Method
The app.param() method is basically used for adding the callback triggers to the route parameters, where name represents the name of the parameter or an array of them and callback represents the callback function.
Syntax
app.param([name], callback)
Parameters
name − Represents the name of the parameter or array of parameters as required.
callback − Represents the callback function. The parameters of the callback function include the request object, the response object, the next middleware, the value of the parameter, and the name of the parameter in the same order.
Example
Create a file with the name "appParam.js" and copy the following code snippet. After creating the file, use the command "node appParam.js" to run this code.
// app.param() Method Demo Example
// Importing the express module
const express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
app.param('id', function (req, res, next, id) {
console.log('app.param is called');
next();
});
app.get('/api/:id', function (req, res, next) {
console.log('Welcome to Tutorials Point!');
next();
});
app.get('/api/:id', function (req, res) {
console.log('SIMPLY LEARNING');
res.end();
});
// App listening on the below port
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Now, hit the following Endpoint with a GET request
http://localhost:3000/api/21
Output
C:\home\node>> node appParam.js Server listening on PORT 3000 app.param is called Welcome to Tutorials Point! SIMPLY LEARNING
