Bird
0
0

What will be the output of this Node.js code? const { URL } = require('url'); const base = 'https://example.com/folder/'; const url = new URL('page.html', base); console.log(url.href);

medium📝 Predict Output Q4 of 15
Node.js - URL and Query String Handling
What will be the output of this Node.js code? const { URL } = require('url'); const base = 'https://example.com/folder/'; const url = new URL('page.html', base); console.log(url.href);
Apage.html
Bhttps://example.com/page.html
Chttps://example.com/folder/
Dhttps://example.com/folder/page.html
Step-by-Step Solution
Solution:
  1. Step 1: Understand URL resolution with base

    The relative path 'page.html' is resolved relative to the base 'https://example.com/folder/'.
  2. Step 2: Combine base and relative path

    Since base ends with '/', the relative path appends to it, resulting in 'https://example.com/folder/page.html'.
  3. Final Answer:

    https://example.com/folder/page.html -> Option D
  4. Quick Check:

    Relative path appended to base folder = D [OK]
Quick Trick: Base URL ending with '/' appends relative path directly [OK]
Common Mistakes:
  • Ignoring trailing slash in base URL
  • Assuming relative path replaces base path

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes