- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
req.params Property in Express.js
The req.params property is an object that contains the properties which are mapped to the named route "parameters". For example, if you have a route as /api/:name, then the "name" property is available as req.params.name. The default value of this object is {}.
Syntax
req.params
Example 1
Create a file with the name "reqParams.js" and copy the following code snippet. After creating the file, use the command "node reqParams.js" to run this code as shown in the example below −
// req.params Property Demo Example // Importing the express var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Defining an Endpoint app.get('/api/:name', function (req, res) { console.log(req.params['name']); res.send(req.params['name']); }); app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Hit the following Endpoint with a GET request −
localhost:3000/api/TutorialsPoint
Output
C:\home
ode>> node reqParams.js Server listening on PORT 3000 TutorialsPoint
Example 2
Let's take a look at one more example.
// req.params Property Demo Example // Importing the express var express = require('express'); // Initializing the express and port number var app = express(); // Initializing the router from express var router = express.Router(); var PORT = 3000; // Defining an Endpoint app.get('/api/:name/:tagLine', function (req, res) { console.log("Name : ", req.params['name']); console.log("TagLine : ", req.params['tagLine']) res.send(req.params['name']); }); app.listen(PORT, function(err){ if (err) console.log(err); console.log("Server listening on PORT", PORT); });
Hit the following Endpoint with a GET Request −
localhost:3000/api/TutorialsPoint/SIMPLY-LEARNING
Output
C:\home
ode>> node reqParams.js Server listening on PORT 3000 Name : TutorialsPoint TagLine : SIMPLY-LEARNING
Advertisements