Bird
Raised Fist0
Node.jsframework~20 mins

URL class for parsing in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
URL Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this URL parsing code?
Consider the following Node.js code using the URL class. What will be logged to the console?
Node.js
import { URL } from 'url';
const myUrl = new URL('https://example.com:8080/path/page?name=chatgpt&lang=en#section1');
console.log(myUrl.hostname);
console.log(myUrl.port);
console.log(myUrl.pathname);
console.log(myUrl.searchParams.get('name'));
console.log(myUrl.hash);
A
"example.com"
"8080"
"path/page"
"chatgpt"
"#section1"
B
"https://example.com"
"8080"
"/path/page"
"chatgpt"
"section1"
C
"example.com"
""
"/path/page"
"chatgpt"
"#section1"
D
"example.com"
"8080"
"/path/page"
"chatgpt"
"#section1"
Attempts:
2 left
💡 Hint
Remember that the URL class separates hostname, port, pathname, search parameters, and hash exactly as in the URL string.
component_behavior
intermediate
2:00remaining
Which option correctly adds a query parameter to a URL?
Given a URL object, which code snippet correctly adds a new query parameter 'page=2' without overwriting existing parameters?
Node.js
import { URL } from 'url';
const url = new URL('https://example.com/search?query=nodejs');
A
url.searchParams.append('page', '2');
console.log(url.toString());
B
url.searchParams.set('page', '2');
console.log(url.toString());
C
url.searchParams = 'page=2';
console.log(url.toString());
D
url.search = '?page=2';
console.log(url.toString());
Attempts:
2 left
💡 Hint
Appending adds a parameter without removing existing ones. Setting replaces or adds a parameter.
📝 Syntax
advanced
2:00remaining
Which option causes a TypeError when creating a URL object?
Which of the following URL constructor usages will cause a TypeError?
Anew URL('://missing.scheme.com');
Bnew URL('ftp://user:pass@example.com:21/path');
Cnew URL('http://example.com');
Dnew URL('https://example.com/path?query=1#frag');
Attempts:
2 left
💡 Hint
A valid URL must have a scheme like http or https.
🔧 Debug
advanced
2:00remaining
Why does this code throw a TypeError?
Analyze the code below. Why does it throw a TypeError?
Node.js
import { URL } from 'url';
const url = new URL('https://example.com');
url.searchParams.get('page').toUpperCase();
ABecause searchParams is not a valid property of URL.
BBecause URL constructor requires a base URL, missing here.
CBecause 'page' parameter does not exist, get() returns null, and calling toUpperCase() on null causes TypeError.
DBecause toUpperCase() is not a function on string.
Attempts:
2 left
💡 Hint
Check what happens when you call get() on a missing parameter.
🧠 Conceptual
expert
3:00remaining
What is the value of url.origin after this code runs?
Consider this code snippet. What will be the value of url.origin?
Node.js
import { URL } from 'url';
const url = new URL('https://user:pass@example.com:8443/path?query=1#frag');
A"https://user:pass@example.com:8443"
B"https://example.com:8443"
C"https://example.com"
D"https://user@example.com:8443"
Attempts:
2 left
💡 Hint
The origin includes scheme, hostname, and port but excludes username and password.

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

  1. 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.
  2. Step 2: Compare with other options

    Creating servers, encrypting data, and managing file paths are unrelated to URL parsing.
  3. Final Answer:

    Break down and work with parts of a web address easily -> Option A
  4. 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

  1. 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).
  2. 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.
  3. Final Answer:

    const url = new URL('https://example.com/path?name=abc'); -> Option C
  4. 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

  1. Step 1: Understand URL properties

    hostname gives domain without port, port gives port number, pathname gives path starting with '/', hash includes '#' plus fragment.
  2. Step 2: Match values from the URL

    Hostname is 'example.com', port is '8080', pathname is '/path/page', hash is '#section'.
  3. Final Answer:

    example.com 8080 /path/page #section -> Option D
  4. Quick Check:

    URL parts match output A [OK]
Hint: hostname excludes port; hash includes '#' [OK]
Common Mistakes:
  • Including port in hostname
  • Missing leading slash in pathname
  • Omitting '#' in hash
4. Consider this code snippet:
const url = new URL('https://example.com/path');
url.hostname = 'newsite.com';
url.port = 3000;
url.pathname = 'newpath';
console.log(url.href);

What is the output?
medium
A. https://newsite.com:3000//newpath
B. https://newsite.com:3000/newpath
C. https://newsite.com:3000/path
D. https://newsite.com:3000/newpath/

Solution

  1. Step 1: Understand pathname assignment

    Setting url.pathname = 'newpath' normalizes the path by adding a leading slash, resulting in '/newpath'.
  2. 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'.
  3. Final Answer:

    https://newsite.com:3000/newpath -> Option B
  4. 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

  1. Step 1: Identify how to update query parameters

    The URL class provides searchParams with methods like set() to update query parameters safely.
  2. 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.
  3. 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
  4. Quick Check:

    Use searchParams.set() to update query [OK]
Hint: Use url.searchParams.set() to change query values [OK]
Common Mistakes:
  • Trying to set query directly as object
  • Using non-existent methods like setQuery
  • Assigning query parameters without searchParams