Bird
0
0

You want to extract the domain and all query parameters from a URL string in Node.js. Which approach correctly achieves this using the WHATWG URL API?

hard📝 Conceptual Q8 of 15
Node.js - URL and Query String Handling
You want to extract the domain and all query parameters from a URL string in Node.js. Which approach correctly achieves this using the WHATWG URL API?
Aconst myURL = new URL(urlString); const domain = myURL.hostname; const params = Object.fromEntries(myURL.searchParams);
Bconst myURL = url.parse(urlString); const domain = myURL.host; const params = myURL.query;
Cconst myURL = new URL(urlString); const domain = myURL.host; const params = myURL.searchParams.toString();
Dconst myURL = new URL(urlString); const domain = myURL.origin; const params = myURL.searchParams.getAll();
Step-by-Step Solution
Solution:
  1. Step 1: Extract domain correctly

    myURL.hostname returns the domain without port, which is usually desired.
  2. Step 2: Convert searchParams to object

    Object.fromEntries(myURL.searchParams) converts query parameters into a plain object for easy use.
  3. Final Answer:

    Use new URL(), get hostname, and convert searchParams to object. -> Option A
  4. Quick Check:

    Extract domain and params with hostname and Object.fromEntries [OK]
Quick Trick: Use Object.fromEntries(searchParams) to get query object [OK]
Common Mistakes:
  • Using url.parse instead of new URL()
  • Using host instead of hostname for domain
  • Not converting searchParams to object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes