Bird
0
0

Identify the error in this Node.js code snippet that tries to parse a URL:

medium📝 Debug Q14 of 15
Node.js - URL and Query String Handling
Identify the error in this Node.js code snippet that tries to parse a URL:
const url = new URL('htp://example.com');
console.log(url.hostname);
AURL class cannot parse URLs with hostnames.
BThe protocol 'htp' is invalid and causes a TypeError.
CMissing 'new' keyword before URL constructor.
DThe console.log statement is incorrect syntax.
Step-by-Step Solution
Solution:
  1. Step 1: Check the URL string protocol

    The protocol 'htp' is misspelled; valid protocols are 'http', 'https', etc.
  2. Step 2: Understand error caused by invalid protocol

    Node.js URL constructor throws a TypeError for invalid protocols like 'htp'.
  3. Final Answer:

    The protocol 'htp' is invalid and causes a TypeError. -> Option B
  4. Quick Check:

    Invalid protocol causes TypeError [OK]
Quick Trick: Check protocol spelling carefully to avoid errors [OK]
Common Mistakes:
  • Ignoring protocol typos
  • Thinking 'new' keyword is missing
  • Assuming console.log syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes