If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले topic में आपने ExpressJS में routing के बारे में सीख और समझा। लेकिन वह static URLs थे मतलब आपने जिस नाम से आपने उन्हें define किया है उन्ही name के साथ access कर सकते हैं। हालाँकि need के according कभी कभी हमें URL के साथ value pass करनी पड जाती है।
ExpressJS में आप POST type की request के साथ तो parameters pass कर ही सकते हैं , इसके साथ साथ GET type की request के लिए dynamic parameters set और get कर सकते हैं। इस topic में आज आप वही सीखेंगे।
Parameter name define करने के लिए route define करते समय ही colon : के साथ parameter का name define किया जाता है। फिर आप request.params property की help से उन values को access कर सकते हैं। request.params आपको pass की गयी values का एक object return करता है।
app.get('/user/:name', function(req, res){ res.send('User Name : ' + req.params.name); });
// import express module.
const express = require('express');
const app = express();
const port = 8000;
// define route
app.get('/:name', function (req, res) {
console.log("Parameters", req.params);
res.send(`User Name : ${req.params.name}`);
});
//listen server.
app.listen(port, function () {
console.log(`Server is running at http://localhost:${port}`);
});
server restart करने के बाद localhost:8000/Rahul hit करेंंगे तो आप कुछ इस तरह से Output पाएंगे।
C:\Users\HP\Desktop\expressjs\test>node app.js Server is running at http://localhost:8000 Parameters { name: 'Rahul' }
ये dynamic parameters थे , अब देखते हैं कि URLs में query string values की कैसे handle करें। Well query string values को आप request.query property की help से access कर सकते हैं।
// import express module.
const express = require('express');
const app = express();
const port = 8000;
// define route
app.get('/:name', function (req, res) {
console.log("Parameters", req.params);
res.send(`User Name : ${req.params.name}`);
});
//listen server.
app.listen(port, function () {
console.log(`Server is running at http://localhost:${port}`);
});
server restart करने के बाद http://localhost:8000/?name=rahul&id=8978 hit करेंंगे तो आप कुछ इस तरह से Output पाएंगे।
C:\Users\HP\Desktop\expressjs\test>node app.js Server is running at http://localhost:8000 Query String Parameters { name: 'rahul', id: '8978' }
I Hope, अब आप ExpressJS में URL buildingसीख गए होंगे।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook