अंतर्निहित यूआरएल मॉड्यूल
यूआरएल मॉड्यूल यूआरएल रिज़ॉल्यूशन और पार्सिंग के लिए उपयोगिताएँ प्रदान करता है।
इसका उपयोग वेब पते को पढ़ने योग्य भागों में विभाजित करने, यूआरएल बनाने और विभिन्न यूआरएल तत्वों में हेरफेर करने के लिए किया जा सकता है।
यूआरएल पार्सिंग
यूआरएल को पढ़ने योग्य भागों में विभाजित करना
यूआरएल संरचना
नए यूआरएल बनाना
यूआरएल तत्वों में हेरफेर करना
विभिन्न यूआरएल भागों को बदलना
शुरू करना
URL ब्लॉक जोड़ने के लिए, require() विधि का उपयोग करें।
आधुनिक Node.js (v10.0.0+) में, आप पारंपरिक API या नए URL वर्ग (WHATWG URL API) का उपयोग कर सकते हैं:
उदाहरण
// Using the legacy API
const url = require('url');
// Using the modern URL class (WHATWG API)
const { URL } = require('url');
उदाहरण
let url = require('url');
let adr = 'http://localhost:8080/default.htm?year=2017&month=february';
let q = url.parse(adr, true);
console.log(q.host);
console.log(q.pathname);
console.log(q.search);
let qdata = q.query;
console.log(qdata.month);
व्याख्या:
url.parse() , URL .
यूआरएल पार्सिंग और फ़ॉर्मेटिंग
यूआरएल ऑब्जेक्ट गुण
जब आप किसी URL को पार्स करते हैं, तो आपको निम्नलिखित गुणों वाला एक URL ऑब्जेक्ट मिलता है:
| प्रवृत्ति | व्याख्या | उदाहरण |
|---|---|---|
| href | पूर्ण पार्स किया गया यूआरएल | 'http://localhost:8080/default.htm?year=2017' |
| protocol | आचार संहिता | 'http:' |
| host | संपूर्ण यजमान का भाग | 'localhost:8080' |
| hostname | होस्टनाम भाग | 'localhost' |
| port | यदि निर्दिष्ट हो तो पोर्ट नंबर | '8080' |
| pathname | URL का पथ खंड | '/default.htm' |
| search | प्राथमिकता? क्वेरी स्ट्रिंग सहित | '?year=2017&month=february' |
| query | ? क्वेरी स्ट्रिंग या पार्स किए गए क्वेरी ऑब्जेक्ट के बिना | 'year=2017&month=february' |
| hash | #सहित खंड पहचानकर्ता | '#section' |
पारंपरिक एपीआई बनाम WHATWG यूआरएल एपीआई
उदाहरण
const { URL } = require('url');
// Using the WHATWG URL API (recommended for new code)
const myURL = new URL('https://example.org:8080/p/a/t/h?query=string#hash');
console.log(myURL.hostname); // 'example.org'
console.log(myURL.pathname); // '/p/a/t/h'
console.log(myURL.searchParams.get('query')); // 'string'
// Using the legacy API
const parsedUrl = require('url').parse('https://example.org:8080/p/a/t/h?query=string#hash');
console.log(parsedUrl.host); // 'example.org:8080'
console.log(parsedUrl.query); // 'query=string'
सिफारिश:
नए कोड के लिए WHATWG URL API की अनुशंसा की जाती है क्योंकि यह आधुनिक वेब मानकों के अनुकूल है और इसमें बेहतर सुरक्षा सुविधाएँ हैं।
URLSearchParams API
URLSearchParams API URL की क्वेरी स्ट्रिंग के साथ काम करने के लिए उपयोगिता विधियाँ प्रदान करता है:
उदाहरण
const { URL, URLSearchParams } = require('url');
const myURL = new URL('https://example.com/?name=Kai&age=30');
const params = new URLSearchParams(myURL.search);
// Get a parameter
console.log(params.get('name')); // 'Kai'
// Add a parameter
params.append('city', 'Stavanger');
// Delete a parameter
params.delete('age');
// Convert to string
console.log(params.toString()); // 'name=Kai&city=Stavanger'
Node.js फ़ाइल सर्वर
अब जब हम जानते हैं कि क्वेरी स्ट्रिंग को कैसे पार्स करना है, तो हमने पिछले अध्याय में सीखा कि Node.js को फ़ाइल सर्वर के रूप में कैसे कार्य किया जाए।
हम दोनों को जोड़ते हैं और क्लाइंट द्वारा अनुरोधित फ़ाइल प्रदान करते हैं।
दो HTML फ़ाइलें बनाएं और उन्हें अपनी नोड.जेएस फ़ाइलों के समान फ़ोल्डर में सहेजें।
summer.html
<!DOCTYPE html>
<html>
<body>
<h1>Summer</h1>
<p>I love the sun!</p>
</body>
</html>
winter.html
<!DOCTYPE html>
<html>
<body>
<h1>Winter</h1>
<p>I love the snow!</p>
</body>
</html>
demo_fileserver.js:
let http = require('http');
let url = require('url');
let fs = require('fs');
http.createServer(function (req, res) {
let q = url.parse(req.url, true);
let filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
फ़ाइल प्रारंभ करें:
C:\Users\Your Name>node demo_fileserver.js
परिणाम:
यदि आप अपने कंप्यूटर पर समान चरणों का पालन करते हैं, तो आपको इन दोनों पतों को खोलते समय दो अलग-अलग परिणाम देखने चाहिए:
http://localhost:8080/summer.html- यह परिणाम उत्पन्न करता है:
<h1>Summer</h1>
<p>I love the sun!</p>
http://localhost:8080/winter.html- यह परिणाम उत्पन्न करता है:
<h1>Winter</h1>
<p>I love the snow!</p>
सर्वोत्तम अभ्यास
1. यूआरएल को नियमित रूप से जांचें और परिष्कृत करें
उदाहरण
function isValidHttpUrl(string) {
try {
const url = new URL(string);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch (err) {
return false;
}
}
console.log(isValidHttpUrl('https://example.com')); // true
console.log(isValidHttpUrl('ftp://example.com')); // false
2. यूआरएल को सुरक्षित रूप से कॉन्फ़िगर करना
उदाहरण
const { URL } = require('url');
// Safe way to construct URLs
function createProfileUrl(domain, username) {
return new URL(`/users/${encodeURIComponent(username)}`, domain).href;
}
console.log(createProfileUrl('https://example.com', 'johndoe'));
// 'https://example.com/users/johndoe'
3. क्वेरी पैरामीटर्स को संभालना
उदाहरण
const { URL } = require('url');
// Parse URL with query parameters
const url = new URL('https://example.com/search?q=node.js⟨=en');
// Get all parameters
console.log(url.searchParams.toString()); // 'q=node.js⟨=en'
// Get specific parameter
console.log(url.searchParams.get('q')); // 'node.js'
// Check if parameter exists
console.log(url.searchParams.has('lang')); // true
// Add new parameter
url.searchParams.append('page', '2');
console.log(url.href);
// 'https://example.com/search?q=node.js⟨=en&page=2'
सुरक्षा नोट:
- हमेशा उपयोगकर्ता इनपुट यूआरएल जांचें
- URL निर्माण के लिए encodeURIComponent() का उपयोग करें
- उचित त्रुटि प्रबंधन लागू करें
- यूआरएल रीडायरेक्ट से निपटते समय सावधान रहें
अभ्यास
होस्टनाम और पोर्ट वाले यूआरएल ऑब्जेक्ट का सही विशेषता नाम खींचें और छोड़ें।
The ______ property.