Bird
Raised Fist0
Node.jsframework~10 mins

Building URLs programmatically in Node.js - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new URL object with the base 'https://example.com'.

Node.js
const url = new URL([1]);
Drag options to blanks, or click blank then click option'
A'http://localhost'
B'https://example.com'
C'ftp://fileserver.com'
D'example.com'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the protocol in the URL string.
Using a relative URL without a base.
2fill in blank
medium

Complete the code to add a query parameter 'search' with value 'books' to the URL.

Node.js
url.searchParams.append([1], 'books');
Drag options to blanks, or click blank then click option'
A'type'
B'query'
C'filter'
D'search'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter key like 'query' or 'filter'.
Forgetting quotes around the key string.
3fill in blank
hard

Fix the error in the code to correctly set the pathname to '/products'.

Node.js
url.[1] = '/products';
Drag options to blanks, or click blank then click option'
Apath
Burl
Cpathname
Dlocation
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'path' instead of 'pathname'.
Trying to set 'url' or 'location' properties.
4fill in blank
hard

Fill both blanks to create a URL with base 'https://api.example.com' and add a query parameter 'page' with value '2'.

Node.js
const url = new URL([1]);
url.searchParams.append([2], '2');
Drag options to blanks, or click blank then click option'
A'https://api.example.com'
B'page'
C'limit'
D'offset'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong base URL or missing protocol.
Using incorrect query parameter keys like 'limit' or 'offset'.
5fill in blank
hard

Fill all three blanks to create a URL with base 'https://shop.com', set pathname to '/items', and add query parameter 'category' with value 'books'.

Node.js
const url = new URL([1]);
url.[2] = '/items';
url.searchParams.append([3], 'books');
Drag options to blanks, or click blank then click option'
A'https://shop.com'
Bpathname
C'category'
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'path' instead of 'pathname'.
Forgetting quotes around strings.
Using wrong query parameter keys.

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