0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
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.