0
0
Node.jsframework~10 mins

Relative vs absolute URL resolution in Node.js - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new URL object with an absolute URL string.

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

Complete the code to resolve a relative URL against a base URL.

Node.js
const base = new URL('https://example.com/folder/');
const resolved = new URL([1], base);
Drag options to blanks, or click blank then click option'
A'/file.txt'
B'folder/file.txt'
C'file.txt'
D'https://other.com/file'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an absolute URL string as the relative URL.
Starting the relative URL with a slash when you want to append.
3fill in blank
hard

Fix the error in the code to correctly create a URL from a relative path and base URL.

Node.js
const base = new URL('https://example.com/dir/');
const url = new URL([1], base);
Drag options to blanks, or click blank then click option'
A'file.txt'
B'/dir/file.txt'
C'dir/file.txt'
D'https://example.com/dir/file.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Including the directory name again in the relative path.
Using an absolute URL string as the relative URL.
4fill in blank
hard

Fill both blanks to create a URL object resolving a relative path with a base URL string.

Node.js
const url = new URL([1], [2]);
Drag options to blanks, or click blank then click option'
A'page.html'
B'https://example.com/base/'
C'/page.html'
D'example.com/base/'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a base URL without protocol.
Using a relative path starting with '/' when you want to append.
5fill in blank
hard

Fill all three blanks to create a URL object and extract its pathname and origin.

Node.js
const url = new URL([1], [2]);
const path = url.[3];
Drag options to blanks, or click blank then click option'
A'subdir/file.txt'
B'https://domain.com/root/'
Cpathname
Dorigin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'origin' when the path is needed.
Using a relative path starting with '/' which resets the path.