Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
URL class for parsing
📖 Scenario: You are building a simple tool to analyze website addresses (URLs). You want to break down a URL into parts like the protocol, hostname, and pathname.
🎯 Goal: Create a Node.js script that uses the built-in URL class to parse a given URL string and extract its components.
📋 What You'll Learn
Create a variable with a URL string
Create a URL object from the string
Extract the protocol, hostname, and pathname from the URL object
Print or store these parts for further use
💡 Why This Matters
🌍 Real World
Parsing URLs is common when working with web addresses, APIs, or routing in web applications.
💼 Career
Understanding how to use the URL class helps in backend and frontend development for handling links, redirects, and data extraction from URLs.
Progress0 / 4 steps
1
Create a URL string variable
Create a variable called myUrl and set it to the string "https://example.com/path/page.html".
Node.js
Hint
Use const to create a variable named myUrl and assign the URL string to it.
2
Create a URL object from the string
Create a variable called parsedUrl and set it to a new URL object created from the myUrl string.
Node.js
Hint
Use new URL(myUrl) to create the URL object and assign it to parsedUrl.
3
Extract protocol, hostname, and pathname
Create three variables: protocol, hostname, and pathname. Assign each the corresponding property from parsedUrl: protocol, hostname, and pathname.
Node.js
Hint
Use dot notation to get protocol, hostname, and pathname from parsedUrl.
4
Log the extracted parts
Add three console.log statements to print protocol, hostname, and pathname each on its own line.
Node.js
Hint
Use console.log() to print each variable on its own line.
Practice
(1/5)
1. What does the URL class in Node.js primarily help you do?
easy
A. Break down and work with parts of a web address easily
B. Create new web servers
C. Encrypt data sent over the internet
D. Manage file system paths
Solution
Step 1: Understand the purpose of the URL class
The URL class is designed to parse and handle web addresses, making it easy to access parts like hostname, pathname, and query.
Step 2: Compare with other options
Creating servers, encrypting data, and managing file paths are unrelated to URL parsing.
Final Answer:
Break down and work with parts of a web address easily -> Option A
Quick Check:
URL class = parse web address [OK]
Hint: URL class = split web address parts easily [OK]
Common Mistakes:
Confusing URL class with server creation
Thinking URL class encrypts data
Mixing URL class with file system modules
2. Which of the following is the correct way to create a new URL object for the address https://example.com/path?name=abc in Node.js?
easy
A. const url = url.parse('https://example.com/path?name=abc');
B. const url = URL('https://example.com/path?name=abc');
C. const url = new URL('https://example.com/path?name=abc');
D. const url = new URL.parse('https://example.com/path?name=abc');
Solution
Step 1: Recall the correct syntax for creating a URL object
The URL class requires the new keyword and a string argument: new URL(string).
Step 2: Check each option
const url = new URL('https://example.com/path?name=abc'); uses correct syntax. const url = URL('https://example.com/path?name=abc'); misses new. const url = url.parse('https://example.com/path?name=abc'); uses old url.parse method, not the URL class. const url = new URL.parse('https://example.com/path?name=abc'); incorrectly combines new and URL.parse.
Final Answer:
const url = new URL('https://example.com/path?name=abc'); -> Option C
Quick Check:
Use new URL() to create URL objects [OK]
Hint: Always use 'new URL()' to create URL objects [OK]
Common Mistakes:
Omitting the 'new' keyword
Using old url.parse() instead of URL class
Trying to call URL as a function without 'new'
3. What will be the output of this Node.js code?
const url = new URL('https://example.com:8080/path/page?query=123#section');
console.log(url.hostname);
console.log(url.port);
console.log(url.pathname);
console.log(url.hash);
medium
A. example.com
/path/page
section
B. https://example.com
8080
path/page
section
C. example.com:8080
/path/page
#section
D. example.com
8080
/path/page
#section
Solution
Step 1: Understand URL properties
hostname gives domain without port, port gives port number, pathname gives path starting with '/', hash includes '#' plus fragment.
Step 2: Match values from the URL
Hostname is 'example.com', port is '8080', pathname is '/path/page', hash is '#section'.
Final Answer:
example.com
8080
/path/page
#section -> Option D
Quick Check:
URL parts match output A [OK]
Hint: hostname excludes port; hash includes '#' [OK]
Setting url.pathname = 'newpath' normalizes the path by adding a leading slash, resulting in '/newpath'.
Step 2: Construct the full URL
Hostname changes to 'newsite.com', port to '3000', pathname becomes '/newpath'. So full URL is 'https://newsite.com:3000/newpath'.
Final Answer:
https://newsite.com:3000/newpath -> Option B
Quick Check:
Pathname setter normalizes with leading '/' [OK]
Hint: pathname setter adds leading '/' automatically [OK]
Common Mistakes:
Expecting '//newpath' without leading slash
Assuming pathname auto-adds slash
Confusing pathname with href
5. You want to change the query parameter id to 42 in this URL: https://shop.com/products?category=books&id=10. Which code correctly updates the URL using the URL class?
hard
A. const url = new URL('https://shop.com/products?category=books&id=10');
url.searchParams.set('id', '42');
console.log(url.href);
B. const url = new URL('https://shop.com/products?category=books&id=10');
url.query.id = 42;
console.log(url.href);
C. const url = new URL('https://shop.com/products?category=books&id=10');
url.search.id = '42';
console.log(url.href);
D. const url = new URL('https://shop.com/products?category=books&id=10');
url.setQuery('id', '42');
console.log(url.href);
Solution
Step 1: Identify how to update query parameters
The URL class provides searchParams with methods like set() to update query parameters safely.
Step 2: Check each option's method
const url = new URL('https://shop.com/products?category=books&id=10');
url.searchParams.set('id', '42');
console.log(url.href); uses searchParams.set(), which is correct. Options A, C, and D use invalid properties or methods not available on URL objects.
Final Answer:
const url = new URL('https://shop.com/products?category=books&id=10');
url.searchParams.set('id', '42');
console.log(url.href); -> Option A
Quick Check:
Use searchParams.set() to update query [OK]
Hint: Use url.searchParams.set() to change query values [OK]