Bird
0
0

Find the mistake in this code: const { URL } = require('url'); const base = 'https://example.com/folder'; const url = new URL('page.html', base); console.log(url.href);

medium📝 Debug Q7 of 15
Node.js - URL and Query String Handling
Find the mistake in this code: const { URL } = require('url'); const base = 'https://example.com/folder'; const url = new URL('page.html', base); console.log(url.href);
AConsole.log syntax error
BBase URL missing trailing slash
CURL class not imported correctly
DRelative path should be absolute
Step-by-Step Solution
Solution:
  1. Step 1: Check base URL format

    The base URL 'https://example.com/folder' lacks a trailing slash, so 'page.html' replaces 'folder'.
  2. Step 2: Understand URL resolution effect

    Without trailing slash, the relative path replaces the last segment, resulting in 'https://example.com/page.html'.
  3. Final Answer:

    Base URL missing trailing slash -> Option B
  4. Quick Check:

    Trailing slash affects relative URL resolution = D [OK]
Quick Trick: Base URL must end with '/' to append relative paths [OK]
Common Mistakes:
  • Ignoring trailing slash in base URL
  • Expecting relative path to append instead of replace

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes