Beginner LevelNode JS
How does the native node:url module parse URLs using the WHATWG URL standard?
❓ Interview Prompt: Explain `node:url` module, WHATWG URL class (`new URL()`), parsing query parameters (`URLSearchParams`), and relative URL resolution.
Node.js implements the standard WHATWG URL API ('new URL()') matching modern browser standards:
•
'url.searchParams': Provides a 'URLSearchParams' object to easily get, set, append, and delete query string parameters.
Code Implementation Example
javascript
import { URL } from 'node:url';
const myURL = new URL('https://api.example.com:8080/v1/users?role=admin&sort=asc#top');
console.log('Hostname:', myURL.hostname);
console.log('Pathname:', myURL.pathname);
console.log('Role Param:', myURL.searchParams.get('role')); // 'admin'
myURL.searchParams.append('limit', '10');
console.log('Updated URL:', myURL.toString());💡Parsing and mutating URL query parameters using standard WHATWG URL.
🎯 Interview Key Takeaway
Use the standard URL class and URLSearchParams to parse and manipulate URLs in Node.js.
#URL#WHATWG#URLSearchParams#Beginner
Back to all Node JS Questions→