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
Recall & Review
beginner
What is an absolute URL?
An absolute URL is a full web address that includes the protocol (like http:// or https://), domain name, and the full path to a resource. It points to the exact location on the internet.
Click to reveal answer
beginner
What is a relative URL?
A relative URL points to a resource based on the current page's location. It does not include the domain or protocol, just the path relative to where you are.
Click to reveal answer
intermediate
How does Node.js resolve a relative URL against a base URL?
Node.js uses the URL constructor to combine a relative URL with a base absolute URL, producing a full absolute URL by filling in missing parts from the base.
Click to reveal answer
beginner
Example: What is the absolute URL of 'images/photo.jpg' if the base URL is 'https://example.com/gallery/'?
The absolute URL is 'https://example.com/gallery/images/photo.jpg'. The relative path is added to the base URL's path.
Click to reveal answer
beginner
Why is understanding relative vs absolute URLs important in web development?
It helps in correctly linking resources like images, scripts, and pages. Using the right URL type ensures links work no matter where the site is hosted or moved.
Click to reveal answer
Which of the following is an absolute URL?
A../page
B/page
Cpage.html
Dhttps://site.com/page
✗ Incorrect
An absolute URL includes the protocol and domain, like 'https://site.com/page'.
In Node.js, which object helps resolve relative URLs against a base URL?
AHttp
BPath
CURL
DFs
✗ Incorrect
The URL constructor in Node.js resolves relative URLs against a base URL.
If the base URL is 'https://example.com/folder/' and the relative URL is '../file.txt', what is the resolved absolute URL?
Ahttps://example.com/file.txt
Bhttps://example.com/folder/file.txt
Chttps://example.com/folder/../file.txt
Dhttps://example.com/folder/../folder/file.txt
✗ Incorrect
The '..' moves one folder up, so the file is at 'https://example.com/file.txt'.
Which URL type is best to use when linking to resources within the same website?
AAbsolute URL
BRelative URL
CBoth are equally good
DNeither
✗ Incorrect
Relative URLs are easier to maintain within the same site and adapt if the domain changes.
What happens if you use an absolute URL in your code and the domain changes?
ALinks break because they point to the old domain
BLinks automatically update
CLinks become relative
DNothing changes
✗ Incorrect
Absolute URLs hardcode the domain, so if it changes, links break unless updated.
Explain the difference between relative and absolute URLs and how Node.js resolves them.
Think about how a relative path is combined with a base full address.
You got /4 concepts.
Describe a real-life scenario where choosing between relative and absolute URLs matters in a web project.
Consider what happens if you move your website to a new address.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a correct description of a relative URL in Node.js?
easy
A. A URL that includes the protocol and domain name.
B. A URL that depends on a base URL to form a full address.
C. A URL that always starts with 'http://' or 'https://'.
D. A URL that cannot be resolved using the URL class.
Solution
Step 1: Understand relative URL meaning
A relative URL does not include the full path and depends on a base URL to form a complete address.
Step 2: Compare options
A URL that depends on a base URL to form a full address. correctly describes this. Options B and C describe absolute URLs. A URL that cannot be resolved using the URL class. is incorrect because Node.js URL class can resolve relative URLs with a base.
Final Answer:
A URL that depends on a base URL to form a full address. -> Option B
Quick Check:
Relative URL = depends on base URL [OK]
Hint: Relative URLs need a base URL to become complete [OK]
Common Mistakes:
Confusing relative URLs with absolute URLs
Thinking relative URLs include protocol
Believing relative URLs cannot be resolved
2. Which of the following is the correct way to create a new URL object for a relative path '/images/photo.jpg' with base 'https://example.com/gallery/' in Node.js?
easy
A. new URL('/images/photo.jpg', 'https://example.com/gallery/')
B. new URL('https://example.com/gallery/' + '/images/photo.jpg')
C. new URL('images/photo.jpg')
D. new URL('https://example.com/gallery/images/photo.jpg')
Solution
Step 1: Understand URL constructor usage
The URL constructor takes two arguments: the path and the base URL. For relative paths, the first argument is the relative path, and the second is the base.
Step 2: Check each option
new URL('/images/photo.jpg', 'https://example.com/gallery/') correctly uses new URL(relativePath, baseURL). new URL('https://example.com/gallery/' + '/images/photo.jpg') concatenates strings but does not use the URL constructor properly. new URL('images/photo.jpg') misses the base URL. new URL('https://example.com/gallery/images/photo.jpg') uses an absolute URL, not relative.
Final Answer:
new URL('/images/photo.jpg', 'https://example.com/gallery/') -> Option A
Quick Check:
URL(relative, base) = correct syntax [OK]
Hint: Use new URL(relativePath, baseURL) for relative URLs [OK]
Common Mistakes:
Concatenating strings instead of using URL constructor
Omitting the base URL for relative paths
Passing absolute URL as first argument when base is given
3. What will be the output of the following Node.js code?
A. Base URL is missing trailing slash, causing incorrect resolution.
B. Relative URL should start with a slash '/' to be valid.
C. URL constructor requires only one argument for relative URLs.
D. No error; code outputs 'https://example.com/path/file.txt' correctly.
Solution
Step 1: Check base URL format
The base URL 'https://example.com/path' lacks a trailing slash, so it is treated as a file, not a folder.
Step 2: Understand URL resolution effect
Appending 'file.txt' to a base treated as file replaces 'path' with 'file.txt', resulting in 'https://example.com/file.txt', not 'https://example.com/path/file.txt'.
Final Answer:
Base URL is missing trailing slash, causing incorrect resolution. -> Option A
Quick Check:
Base URL trailing slash affects relative URL resolution [OK]
Hint: Always add trailing slash to base URL if it's a folder [OK]
Common Mistakes:
Omitting trailing slash on base URL
Thinking relative URLs must start with '/'
Passing wrong number of arguments to URL constructor
5. You want to combine a base URL 'https://example.com/api/v1/' with a relative URL '../../images/pic.jpg' in Node.js. What will be the resolved absolute URL?
hard
A. https://example.com/api/v1/images/pic.jpg
B. https://example.com/api/images/pic.jpg
C. https://example.com/api/v1/../../images/pic.jpg
The '../../' means go up two folders from 'https://example.com/api/v1/'.
Step 2: Move up two levels from base URL
Starting at 'https://example.com/api/v1/', going up one level is 'https://example.com/api/', and up another is 'https://example.com/'. Then append 'images/pic.jpg'.
Final Answer:
https://example.com/images/pic.jpg -> Option D
Quick Check:
Two '..' moves up two directories [OK]
Hint: Count '..' to move up folders in URL paths [OK]