We use relative and absolute URLs to tell programs where to find web pages or resources. Understanding how Node.js resolves these URLs helps us load the right files or links.
Relative vs absolute URL resolution in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
new URL(input, base);input is the URL string you want to resolve.
base is the base URL used to resolve relative URLs. If input is absolute, base is ignored.
Examples
/path/page.html against the base URL https://example.com/folder/, resulting in https://example.com/path/page.html.Node.js
const url = new URL('/path/page.html', 'https://example.com/folder/'); console.log(url.href);
Node.js
const url = new URL('https://google.com/search');
console.log(url.href);https://example.com/gallery/images/photo.png.Node.js
const url = new URL('images/photo.png', 'https://example.com/gallery/'); console.log(url.href);
Sample Program
This example shows how Node.js resolves different URLs using the URL class. It prints the full resolved URLs for relative and absolute inputs.
Node.js
import { URL } from 'url'; // Relative URL with base const relativeUrl = new URL('docs/tutorial.html', 'https://site.com/start/'); console.log(relativeUrl.href); // Absolute URL ignores base const absoluteUrl = new URL('https://other.com/page'); console.log(absoluteUrl.href); // Relative URL with leading slash replaces path const rootRelativeUrl = new URL('/about', 'https://site.com/start/'); console.log(rootRelativeUrl.href);
Important Notes
Relative URLs without a leading slash append to the base path.
Relative URLs with a leading slash replace the path part of the base URL.
Absolute URLs ignore the base and stand alone.
Summary
Relative URLs depend on a base URL to form a full address.
Absolute URLs are complete and do not need a base.
Node.js URL class helps combine and resolve URLs correctly.
Practice
1. Which of the following is a correct description of a relative URL in Node.js?
easy
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 BQuick 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
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 AQuick 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
Solution
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/'.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.Final Answer:
https://example.com/image.png -> Option CQuick 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
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 AQuick 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
Solution
Step 1: Analyze relative path '../../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 DQuick 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
