← Back to Node.js Course | Chapter 6: HTTP Module | Lesson 5 of 7

URL मॉड्यूल

URL class एक web address को protocol, host, path, और बाकी में तोड़ता है, और नई safely बनाता है।

In this page:

  1. URL Module
Syntax
javascript
const url = new URL(text, base);
url.protocol
url.hostname
url.pathname
url.searchParams

URL Module

new URL(text, base) addresses parse और validate करता है। यह protocol, hostname, port, pathname, search, और hash expose करता है। Properties set करना address को encoding के साथ सही तरीके से rebuild करता है।

Note: एक invalid URL एक TypeError throw करता है, इसलिए untrusted input को try/catch में wrap करें।

उदाहरण: URL module

javascript
const u = new URL("https://[email protected]:8080/a/b?x=1#top");
console.log(u.protocol, u.hostname, u.port);
console.log(u.pathname, u.search, u.hash);
u.searchParams.set("y", "two words");
console.log(u.href);
try { new URL("not a url"); } catch (e) { console.log(e.name); }

// Output:
// https: example.com 8080
// /a/b ?x=1 #top
// https://[email protected]:8080/a/b?x=1&y=two+words#top
// TypeError

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. regular expressions से URLs parse करना
  2. relative URLs के लिए एक base भूल जाना
  3. invalid URLs catch न करना
चैप्टर सारांश
  • URL addresses parse करता है
  • pathname, search, hash expose करता है
  • Relative URLs को एक base चाहिए
  • Invalid input throw करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.