Bird
Raised Fist0
Node.jsframework~20 mins

Building URLs programmatically 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 Mastery Badge
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 construction code?
Consider the following Node.js code using the URL class. What will be logged to the console?
Node.js
const { URL } = require('url');
const base = 'https://example.com/path';
const url = new URL('/subpath?query=123', base);
console.log(url.href);
Ahttps://example.com/path/subpath?query=123
Bhttps://example.com/subpath?query=123
Chttps://example.com/path?query=123
Dhttps://example.com/path/subpath
Attempts:
2 left
💡 Hint
Remember how the URL constructor resolves relative paths against the base URL.
component_behavior
intermediate
2:00remaining
How does URLSearchParams handle multiple values for the same key?
Given the following code, what will be the output of the console.log statement?
Node.js
const params = new URLSearchParams();
params.append('color', 'red');
params.append('color', 'blue');
console.log(params.toString());
Acolor=red,blue
Bcolor=blue
Ccolor=red
Dcolor=red&color=blue
Attempts:
2 left
💡 Hint
URLSearchParams allows multiple values for the same key by appending them.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a URL with a dynamic query parameter?
You want to create a URL 'https://api.example.com/data' with a query parameter 'id' set to a variable value. Which code snippet correctly does this?
A
const url = new URL('https://api.example.com/data');
url.searchParams.set('id', id);
console.log(url.href);
B
const url = new URL('https://api.example.com/data?id=' + id);
console.log(url.href);
C
const url = new URL('https://api.example.com/data');
url.query.id = id;
console.log(url.href);
D
const url = new URL('https://api.example.com/data');
url.search.id = id;
console.log(url.href);
Attempts:
2 left
💡 Hint
Use the URLSearchParams API to set query parameters dynamically.
🔧 Debug
advanced
2:00remaining
Why does this URL constructor code throw an error?
Examine the code below. Why does it throw a TypeError?
Node.js
const { URL } = require('url');
const base = 'https://example.com';
const url = new URL('path', base);
console.log(url.href);
ABecause the base URL must be an instance of URL, not a string.
BBecause the base URL is missing a trailing slash, so relative paths are invalid.
CBecause 'path' is a relative path without a leading slash, it cannot be resolved against the base string.
DBecause the URL constructor requires the first argument to be an absolute URL string.
Attempts:
2 left
💡 Hint
Check how relative paths without leading slashes are resolved against base URLs.
🧠 Conceptual
expert
3:00remaining
What is the effect of setting url.hash in a URL object?
Given the code below, what will be the value of url.href after setting url.hash = '#section2'?
Node.js
const { URL } = require('url');
const url = new URL('https://example.com/page?query=1');
url.hash = '#section2';
console.log(url.href);
Ahttps://example.com/page?query=1#section2
Bhttps://example.com/page#section2?query=1
Chttps://example.com/page?query=1
Dhttps://example.com/page#section2
Attempts:
2 left
💡 Hint
The hash part of a URL comes after the query string and starts with '#'.

Practice

(1/5)
1. What is the main benefit of using the URL class in Node.js when building URLs programmatically?
easy
A. It automatically encodes special characters and manages URL parts safely.
B. It allows direct string concatenation without any checks.
C. It disables query parameters to simplify URLs.
D. It only works with HTTP URLs, ignoring others.

Solution

  1. Step 1: Understand the purpose of the URL class

    The URL class helps build URLs by handling encoding and structure automatically.
  2. Step 2: Compare options with URL class features

    Only It automatically encodes special characters and manages URL parts safely. correctly describes automatic encoding and safe URL part management.
  3. Final Answer:

    It automatically encodes special characters and manages URL parts safely. -> Option A
  4. Quick Check:

    URL class = safe URL building [OK]
Hint: Remember: URL class handles encoding and structure safely [OK]
Common Mistakes:
  • Thinking URL class just concatenates strings
  • Assuming URL class disables query parameters
  • Believing URL class only supports HTTP URLs
2. Which of the following is the correct way to create a new URL object for 'https://example.com' in Node.js?
easy
A. const url = new URL('https://example.com');
B. const url = new URL('example.com');
C. const url = URL('https://example.com');
D. const url = new URL(); url.href = 'https://example.com';

Solution

  1. Step 1: Check URL constructor usage

    The URL constructor requires a full URL string including protocol, e.g., 'https://example.com'.
  2. Step 2: Validate each option

    const url = new URL('https://example.com'); correctly uses new URL with full URL string. const url = new URL('example.com'); misses protocol, C misses 'new', D uses empty constructor which is invalid.
  3. Final Answer:

    const url = new URL('https://example.com'); -> Option A
  4. Quick Check:

    Use new URL('full-url') syntax [OK]
Hint: Always include protocol and use 'new' with URL [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Passing URL without protocol
  • Trying to create URL without constructor
3. What will be the output of this Node.js code?
const url = new URL('https://example.com/path');
url.searchParams.append('q', 'nodejs');
url.searchParams.append('page', '2');
console.log(url.toString());
medium
A. https://example.com/path?page=2&q=nodejs
B. https://example.com/path?q=nodejs&page=2
C. https://example.com/path?q=nodejs&page=2&
D. https://example.com/path

Solution

  1. Step 1: Understand searchParams.append behavior

    Appending 'q=nodejs' then 'page=2' adds these query parameters in order.
  2. Step 2: Check URL string output

    Calling toString() returns full URL with query string '?q=nodejs&page=2' appended to path.
  3. Final Answer:

    https://example.com/path?q=nodejs&page=2 -> Option B
  4. Quick Check:

    Appended params appear in order in URL string [OK]
Hint: searchParams.append adds parameters in order [OK]
Common Mistakes:
  • Assuming parameters order is reversed
  • Expecting trailing '&' at end
  • Ignoring appended query parameters
4. Identify the error in this code snippet that tries to add a query parameter:
const url = new URL('https://example.com');
url.searchParams.add('key', 'value');
console.log(url.href);
medium
A. The 'href' property is read-only and cannot be logged.
B. URL constructor is missing the protocol.
C. You cannot modify searchParams after URL creation.
D. The method 'add' does not exist on searchParams; should use 'append'.

Solution

  1. Step 1: Check searchParams methods

    The correct method to add a query parameter is 'append', not 'add'.
  2. Step 2: Validate other code parts

    URL has protocol, searchParams can be modified, and href can be logged. Only method name is wrong.
  3. Final Answer:

    The method 'add' does not exist on searchParams; should use 'append'. -> Option D
  4. Quick Check:

    Use searchParams.append, not add [OK]
Hint: Use 'append' to add query parameters, not 'add' [OK]
Common Mistakes:
  • Using 'add' instead of 'append'
  • Thinking href is not accessible
  • Believing searchParams is immutable
5. You want to build a URL with base 'https://api.example.com/search' and add parameters 'query' with value 'node.js tips' and 'sort' with value 'recent'. Which code correctly builds this URL with proper encoding?
hard
A. const url = new URL('https://api.example.com/search'); url.searchParams.set('query', 'node.js tips'); url.searchParams.set('sort', 'recent'); console.log(url.toString());
B. const url = 'https://api.example.com/search?query=node.js tips&sort=recent'; console.log(url);
C. const url = new URL('https://api.example.com/search'); url.searchParams.append('query', 'node.js tips'); url.searchParams.append('sort', 'recent'); console.log(url.href);
D. const url = new URL('https://api.example.com/search'); url.searchParams.add('query', 'node.js tips'); url.searchParams.add('sort', 'recent'); console.log(url.href);

Solution

  1. Step 1: Understand URL and searchParams usage

    Use new URL with base URL, then append parameters with searchParams.append to add multiple values safely.
  2. Step 2: Check encoding and method correctness

    const url = new URL('https://api.example.com/search'); url.searchParams.append('query', 'node.js tips'); url.searchParams.append('sort', 'recent'); console.log(url.href); uses append correctly and prints href, which includes proper encoding of spaces as '%20'. const url = new URL('https://api.example.com/search'); url.searchParams.set('query', 'node.js tips'); url.searchParams.set('sort', 'recent'); console.log(url.toString()); uses set which replaces values but also works; however, question asks for correct code with proper encoding and append is more common for adding multiple params. const url = 'https://api.example.com/search?query=node.js tips&sort=recent'; console.log(url); is manual string and misses encoding. const url = new URL('https://api.example.com/search'); url.searchParams.add('query', 'node.js tips'); url.searchParams.add('sort', 'recent'); console.log(url.href); uses non-existent 'add' method.
  3. Final Answer:

    Option C code correctly builds URL with proper encoding and appends parameters. -> Option C
  4. Quick Check:

    Use URL + searchParams.append for safe, encoded URLs [OK]
Hint: Use URL and searchParams.append for safe, encoded query parameters [OK]
Common Mistakes:
  • Manually concatenating query strings without encoding
  • Using non-existent 'add' method
  • Ignoring encoding of spaces and special characters