The URL module provides utilities for URL parsing and manipulation using the WHATWG URL Standard.
URL Class#
Relative URLs#
URLSearchParams#
Creating URLSearchParams#
URL Encoding#
Building URLs#
Validating URLs#
URL Comparison#
File URLs#
Legacy URL Module#
Common Patterns#
Best Practices#
URL Class:
✓ Use WHATWG URL class (new URL)
✓ Handle URL construction errors
✓ Use searchParams for query strings
✓ Normalize URLs for comparison
Security:
✓ Validate URLs before use
✓ Check protocol (http/https)
✓ Sanitize user-provided URLs
✓ Use URL encoding for parameters
Patterns:
✓ Create URL builders for complex URLs
✓ Use relative URLs when possible
✓ Extract common URL operations
✓ Handle edge cases
Avoid:
✗ String concatenation for URLs
✗ Manual query string building
✗ Forgetting to encode values
✗ Using deprecated url.parse
Conclusion#
The Node.js URL module provides robust URL parsing and manipulation following the WHATWG URL Standard. Use the URL class for parsing and constructing URLs, URLSearchParams for query string handling, and fileURLToPath/pathToFileURL for file system integration. Always validate URLs and use proper encoding. The modern URL class should be preferred over the legacy url.parse function.