Bird
Raised Fist0
Node.jsframework~10 mins

Relative vs absolute URL resolution in Node.js - Visual Side-by-Side Comparison

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
Concept Flow - Relative vs absolute URL resolution
Start with base URL
Receive input URL
Is input URL absolute?
YesUse input URL as is
No
Combine base URL with relative input
Normalize combined URL
Return resolved absolute URL
This flow shows how Node.js resolves a URL: it checks if the input is absolute, if not, it combines it with a base URL and normalizes the result.
Execution Sample
Node.js
import { URL } from 'url';
const base = new URL('https://example.com/path/');
const relative = 'sub/page.html';
const resolved = new URL(relative, base);
console.log(resolved.href);
This code resolves a relative URL against a base URL and prints the absolute URL.
Execution Table
StepInput URLBase URLIs Absolute?ActionResulting URL
1'sub/page.html''https://example.com/path/'NoCombine base + relative'https://example.com/path/sub/page.html'
2'https://google.com/search''https://example.com/path/'YesUse input as is'https://google.com/search'
3'../up.html''https://example.com/path/'NoCombine and normalize'https://example.com/up.html'
4'/root.html''https://example.com/path/'NoCombine with root path'https://example.com/root.html'
5'ftp://files.com/file.txt''https://example.com/path/'YesUse input as is'ftp://files.com/file.txt'
💡 Resolution ends after returning the absolute URL based on input type.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
resolved.hrefundefinedhttps://example.com/path/sub/page.htmlhttps://google.com/searchhttps://example.com/up.htmlhttps://example.com/root.htmlftp://files.com/file.txt
Key Moments - 3 Insights
Why does '../up.html' resolve to 'https://example.com/up.html' instead of 'https://example.com/path/../up.html'?
Because the URL resolution normalizes the path by removing '..' and its preceding directory, as shown in step 3 of the execution_table.
What happens if the input URL is already absolute?
The input URL is used as is without combining with the base URL, as shown in steps 2 and 5.
How does a leading slash in the relative URL affect resolution?
A leading slash resets the path to the root of the base URL's domain, demonstrated in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the resolved URL at step 3?
Ahttps://example.com/up.html
Bhttps://example.com/path/up.html
Chttps://example.com/path/../up.html
Dhttps://example.com/path/../path/up.html
💡 Hint
Check the 'Resulting URL' column for step 3 in the execution_table.
At which step does the input URL get used without combining with the base URL?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 'Is Absolute?' = Yes and 'Action' = Use input as is in the execution_table.
If the relative URL was changed from 'sub/page.html' to '/sub/page.html' in step 1, what would the resolved URL be?
Ahttps://example.com/path/sub/page.html
Bhttps://example.com/path//sub/page.html
Chttps://example.com/sub/page.html
Dhttps://example.com/subpage.html
💡 Hint
Recall that a leading slash resets the path to the root, as shown in step 4.
Concept Snapshot
URL resolution in Node.js:
- If input URL is absolute, use it directly.
- If relative, combine with base URL.
- Normalize paths (remove '..' and '.')
- Leading slash resets to domain root
- Use new URL(relativeOrAbsolute, baseURL) to resolve
Full Transcript
This lesson shows how Node.js resolves URLs using the URL constructor. It starts with a base URL and an input URL. If the input is absolute, it uses it directly. If relative, it combines with the base and normalizes the path. Examples include relative paths like 'sub/page.html', parent directory references like '../up.html', and absolute URLs like 'https://google.com/search'. The process ensures the final URL is absolute and normalized for use in applications.

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

  1. 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.
  2. 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.
  3. Final Answer:

    A URL that depends on a base URL to form a full address. -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    new URL('/images/photo.jpg', 'https://example.com/gallery/') -> Option A
  4. 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?
const { URL } = require('url');
const base = 'https://example.com/folder/';
const relative = '../image.png';
const fullUrl = new URL(relative, base);
console.log(fullUrl.href);
medium
A. https://example.com/folder/../image.png
B. Error: Invalid URL
C. https://example.com/image.png
D. https://example.com/folder/image.png

Solution

  1. Step 1: Understand relative path resolution

    The relative path '../image.png' means go one folder up from 'https://example.com/folder/' which leads to 'https://example.com/'.
  2. Step 2: Resolve the full URL

    Combining base and relative path, the URL class normalizes the path to 'https://example.com/image.png'. https://example.com/image.png matches this.
  3. Final Answer:

    https://example.com/image.png -> Option C
  4. Quick Check:

    Relative '..' moves up one directory [OK]
Hint: Relative '..' moves up one folder in URL paths [OK]
Common Mistakes:
  • Not normalizing '..' in URL paths
  • Expecting '../' to stay in the URL string
  • Confusing relative and absolute URL outputs
4. Identify the error in this Node.js code snippet that tries to resolve a relative URL:
const { URL } = require('url');
const base = 'https://example.com/path';
const relative = 'file.txt';
const url = new URL(relative, base);
console.log(url.href);
medium
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

  1. 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.
  2. 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'.
  3. Final Answer:

    Base URL is missing trailing slash, causing incorrect resolution. -> Option A
  4. 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
D. https://example.com/images/pic.jpg

Solution

  1. Step 1: Analyze relative path '../../images/pic.jpg'

    The '../../' means go up two folders from 'https://example.com/api/v1/'.
  2. 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'.
  3. Final Answer:

    https://example.com/images/pic.jpg -> Option D
  4. Quick Check:

    Two '..' moves up two directories [OK]
Hint: Count '..' to move up folders in URL paths [OK]
Common Mistakes:
  • Not moving up enough folders for multiple '..'
  • Appending relative path without normalization
  • Confusing base URL folder levels