0
0
Node.jsframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Resolution 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 resolving a relative URL against a base URL?
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 = new URL('https://example.com/folder/page.html');
const relative = new URL('../image.png', base);
console.log(relative.href);
A"https://example.com/../image.png"
B"https://example.com/folder/image.png"
C"https://example.com/image.png"
D"https://example.com/folder/page.html/image.png"
Attempts:
2 left
💡 Hint
Think about how '..' moves up one folder in a path.
Predict Output
intermediate
2:00remaining
What happens when resolving an absolute URL against a base URL?
What will this Node.js code output?
Node.js
const { URL } = require('url');
const base = new URL('https://example.com/folder/page.html');
const absolute = new URL('https://other.com/path', base);
console.log(absolute.href);
A"https://other.com/path"
B"https://example.com/folder/https://other.com/path"
C"https://example.com/https://other.com/path"
D"https://example.com/folder/path"
Attempts:
2 left
💡 Hint
Absolute URLs ignore the base URL.
component_behavior
advanced
2:30remaining
How does URL resolution handle trailing slashes in base URLs?
Given this code, what will be the output?
Node.js
const { URL } = require('url');
const base1 = new URL('https://example.com/folder/');
const base2 = new URL('https://example.com/folder');
const relative = 'page.html';
console.log(new URL(relative, base1).href);
console.log(new URL(relative, base2).href);
A"https://example.com/page.html" and "https://example.com/folder/page.html"
B"https://example.com/folder/page.html" and "https://example.com/folderpage.html"
C"https://example.com/folder/page.html" and "https://example.com/folder/page.html"
D"https://example.com/folder/page.html" and "https://example.com/page.html"
Attempts:
2 left
💡 Hint
Trailing slash means the base is treated as a folder, no trailing slash means it's a file.
📝 Syntax
advanced
2:00remaining
Which option causes a SyntaxError when creating a URL?
Which of these URL constructor calls will cause a SyntaxError in Node.js?
Anew URL('http://example.com:invalid/path')
Bnew URL('https://example.com/path?query=1')
Cnew URL('http://example.com:80/path')
Dnew URL('ftp://example.com/resource')
Attempts:
2 left
💡 Hint
Ports must be numeric and valid.
🔧 Debug
expert
3:00remaining
Why does this URL resolution produce an unexpected result?
Examine this code snippet and choose the correct explanation for the output.
Node.js
const { URL } = require('url');
const base = new URL('https://example.com/folder/page.html');
const relative = new URL('subfolder/../image.png', base);
console.log(relative.href);
AThe URL constructor throws an error because '..' is not allowed in relative URLs.
BThe '..' in the relative path moves up one directory, so the final URL is 'https://example.com/folder/image.png'.
CThe relative URL is treated as a string and concatenated, resulting in 'https://example.com/folder/page.htmlsubfolder/../image.png'.
DThe relative URL is resolved incorrectly because the base URL must end with a slash to handle '..' properly.
Attempts:
2 left
💡 Hint
Think about how '..' works in paths and how URL resolution normalizes paths.