- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Node.js – Reading Path Parameters from URL
We can embed path variables into the URL and then use these path parameters to retrieve info from resources. These API endpoints have different values with respect to different values passed inside them.
Example 1
Create a file named "index.js" and copy the following code snippet. After creating the file, use the command "node index.js" to run this code.
// Reading Path parameters in Node.js // Importing the below modules const express = require("express") const path = require('path') const app = express() var PORT = process.env.port || 3001 app.get('/p/:tagId', function(req, res) { console.log("TagId received is : " + req.params.tagId); res.send("TagId is set to " + req.params.tagId); }); app.listen(PORT, function(error){ if (error) throw error console.log("Server running successfully on PORT : ", PORT) })
Output
C:\home
ode>> node index.js Server running successfully on PORT: 3001 TagId received is: 1
Example 2
// Reading Path parameters in Node.js // Importing the below modules const express = require("express") const path = require('path') const app = express() var PORT = process.env.port || 3001 app.get('/p/:id/:username/:password', function(req, res) { var user_id = req.params['id'] var username = req.params['username'] var password = req.params['password'] console.log("UserId is : " + user_id + " username is : " + username + " password is : " + password); res.send("UserId is : " + user_id + " username is : " + username + " password is : " + password); }); app.listen(PORT, function(error){ if (error) throw error console.log("Server running successfully on PORT : ", PORT) })
Output
C:\home
ode>> node index.js Server running successfully on PORT : 3001 UserId is : 21 username is : Rahul password is : Rahul@021
- Related Articles
- JavaScript - know the value of GET parameters from URL
- How to get parameters from a URL string in PHP?
- Reading a text file into an Array in Node.js
- How to read request parameters passed in URL using JSP?
- How to get an object containing parameters of current URL in JavaScript?
- Reading UTF8 data from a file using Java
- Serving html pages from node.js
- Reading data from keyboard using console class in Java
- Extract hostname from URL string in JavaScript?
- Reading the contents of SAP structures from outside using RFC
- Value parameters vs Reference parameters vs Output Parameters in C#
- How to download image from url in Android?
- Dynamically creating parameters from table entries in SAP system
- Thermometer Reading Temperature
- Get an Absolute Filename Path from a Relative Filename Path in Java

Advertisements