जवाबों:
req.paramsमार्ग पैरामीटर (URL के पथ भाग में) req.queryशामिल हैं , और URL क्वेरी पैरामीटर (URL के बाद ?) शामिल हैं।
आप req.param(name)दोनों स्थानों (साथ ही req.body) में एक पैरामीटर देखने के लिए उपयोग कर सकते हैं , लेकिन यह विधि अब पदावनत है।
req.paramअब पदावनत हो गया है। नोड का उपयोग कर पता चलता है req.queryयाreq.params
इस मार्ग को दिया
app.get('/hi/:param1', function(req,res){} );
और यह URL दिया
http://www.google.com/hi/there?qs1=you&qs2=tube
आपके पास होगा:
अनुरोध। सवाल
{
qs1: 'you',
qs2: 'tube'
}
अनुरोध। पैरामीटर
{
param1: 'there'
}
/
मान लीजिए कि आपने अपने मार्ग का नाम इस तरह परिभाषित किया है:
https://localhost:3000/user/:userid
जो बन जाएगा:
https://localhost:3000/user/5896544
यहां, यदि आप प्रिंट करेंगे: request.params
{
userId : 5896544
}
इसलिए
request.params.userId = 5896544
इसलिए request.params नामांकित मार्ग के गुणों वाली एक वस्तु है
और request.query URL में क्वेरी पैरामीटर से आता है जैसे:
https://localhost:3000/user?userId=5896544
request.query
{
userId: 5896544
}
इसलिए
request.query.userId = 5896544
अब आपको डॉट नोटेशन का उपयोग करके क्वेरी को एक्सेस करने में सक्षम होना चाहिए।
यदि आप यह कहना चाहते हैं कि आपको एक GET अनुरोध प्राप्त हो रहा है /checkEmail?type=email&utm_source=xxxx&email=xxxxx&utm_campaign=XXऔर आप उपयोग की गई क्वेरी को प्राप्त करना चाहते हैं ।
var type = req.query.type,
email = req.query.email,
utm = {
source: req.query.utm_source,
campaign: req.query.utm_campaign
};
परम को अनुरोध प्राप्त करने के लिए स्व परिभाषित पैरामीटर के लिए उपयोग किया जाता है, जैसे कुछ (उदाहरण):
router.get('/:userID/food/edit/:foodID', function(req, res){
//sample GET request at '/xavg234/food/edit/jb3552'
var userToFind = req.params.userID;//gets xavg234
var foodToSearch = req.params.foodID;//gets jb3552
User.findOne({'userid':userToFind}) //dummy code
.then(function(user){...})
.catch(function(err){console.log(err)});
});
मैं एक महत्वपूर्ण नोट के बारे में उल्लेख करना चाहता हूं req.query, क्योंकि वर्तमान में मैं पृष्ठ पर आधारित कार्यक्षमता पर काम कर रहा हूं req.queryऔर मेरे पास आपके लिए प्रदर्शित करने के लिए एक दिलचस्प उदाहरण है ...
उदाहरण:
// Fetching patients from the database
exports.getPatients = (req, res, next) => {
const pageSize = +req.query.pageSize;
const currentPage = +req.query.currentPage;
const patientQuery = Patient.find();
let fetchedPatients;
// If pageSize and currentPage are not undefined (if they are both set and contain valid values)
if(pageSize && currentPage) {
/**
* Construct two different queries
* - Fetch all patients
* - Adjusted one to only fetch a selected slice of patients for a given page
*/
patientQuery
/**
* This means I will not retrieve all patients I find, but I will skip the first "n" patients
* For example, if I am on page 2, then I want to skip all patients that were displayed on page 1,
*
* Another example: if I am displaying 7 patients per page , I want to skip 7 items because I am on page 2,
* so I want to skip (7 * (2 - 1)) => 7 items
*/
.skip(pageSize * (currentPage - 1))
/**
* Narrow dont the amound documents I retreive for the current page
* Limits the amount of returned documents
*
* For example: If I got 7 items per page, then I want to limit the query to only
* return 7 items.
*/
.limit(pageSize);
}
patientQuery.then(documents => {
res.status(200).json({
message: 'Patients fetched successfully',
patients: documents
});
});
};
आप के +सामने हस्ताक्षर देखा जाएगा req.query.pageSizeऔरreq.query.currentPage
क्यों? यदि आप हटाते हैं+ इस मामले में , तो आपको एक त्रुटि मिलेगी, और उस त्रुटि को फेंक दिया जाएगा क्योंकि हम अमान्य प्रकार (त्रुटि संदेश के साथ 'सीमा' फ़ील्ड संख्यात्मक होना चाहिए) का उपयोग करेंगे।
जरूरी : डिफ़ॉल्ट रूप से यदि आप इन क्वेरी मापदंडों से कुछ निकालते हैं, तो यह हमेशा एक स्ट्रिंग होगा , क्योंकि यह URL आ रहा है और इसे एक पाठ के रूप में माना जाता है।
अगर हमें संख्याओं के साथ काम करना है, और क्वेरी स्टेटमेंट को टेक्स्ट से नंबर में बदलना है, तो हम केवल स्टेटमेंट के सामने एक प्लस साइन जोड़ सकते हैं।