The url module in Node.js provides utilities to parse and manipulate URLs. The URL class is globally available and allows access to URL properties using getters and setters.
- Provides the WHATWG URL class for URL parsing and manipulation.
- The URL class is globally available in Node.js.
- Supports getters and setters to access and modify URL components like protocol, host, and query parameters.
// Create URLSearchParams object
const searchpar = new URLSearchParams("keya=vala&keyb=valb");
// Loop through key-value pairs
for (const [key, value] of searchpar.entries()) {
console.log(key + ', ' + value); // Print key and value
}
Output
keya, vala keyb, valb
Node.js URL Module APIs
Node.js URL Module APIs provide methods and properties to parse, construct, and manipulate URLs in Node.js applications.
URL Methods
- URL(): URL() method provides utilities for URL resolution and parsing.
- url.toString(): url.toString() method returns the URL as a string representation.
- url.toJSON(): url.toJSON() method returns the serialized URL of the URL object.
- pathToFileURL(): pathToFileURL() method converts a file path into a file URL.
- fileURLToPath(): fileURLToPath() method converts a file URL into a path string.
- parse(): parse() method parses a URL string and returns a URL object.
- resolve(): resolve() method resolves a target URL relative to a base URL.
- format(): format() method formats a URL object into a URL string.
- domainToASCII(): domainToASCII() method converts a Unicode domain name to ASCII.
- domainToUnicode(): domainToUnicode() method converts an ASCII domain name to Unicode.
URL Class APIs
- hash: hash API gets or sets the fragment portion of the URL.
- host: host API gets or sets the host portion of the URL.
- origin: origin API returns the origin of the URL.
- username: username API gets or sets the username of the URL.
- search: search API gets or sets the query string of the URL.
- port: port API gets or sets the port number of the URL.
- pathname: pathname API gets or sets the pathname portion of the URL.
- password: password API gets or sets the password of the URL.
- href: href API returns the complete URL string.
- hostname: hostname API gets or sets the hostname portion of the URL.
- protocol: protocol API gets or sets the protocol scheme of the URL.
Class: URLSearchParams
- append(): The append() method adds a key–value pair to the query string.
- delete(): The delete() method removes the specified parameter and its values.
- entries(): The entries() method returns an iterator for all key–value pairs.
- forEach(): The forEach() method iterates through all parameters using a callback function.
- get(): The get() method returns the first value of the specified parameter.
- getAll(): The getAll() method returns all values of a parameter as an array.
- set(): The set() method sets or updates the value of a parameter.
- has(): The has() method checks whether a parameter exists.
- keys(): The keys() method returns an iterator of parameter names.
- values(): The values() method returns an iterator of parameter values.
- sort(): The sort() method sorts parameters using a stable sorting algorithm.
- toString(): The toString() method returns the query string representation.
Legacy urlObject
- auth: The auth property returns the authentication information of the URL.
- hash: The hash property identifies the fragment portion of a URL.
- host: The host property returns the hostname along with the port.
- search: The search property returns the query string starting with ?.
- protocol: The protocol property returns the protocol used in the URL.
- href: The href property returns the complete URL string.
- query: The query property returns the query either as a string or an object.
- slashes: The slashes property indicates whether // appears after the protocol.
- port: The port property returns the port number of the host.
- pathname: The pathname property returns the path portion of the URL

