URL

Node.js url module — URL parsing and formatting utilities. Exposes both the legacy API (parse/format/resolve) and the WHATWG URL class.

Module: node:url Support: Experimental Since: 1.1.0 Docs: Node.js Docs

11 members: 10 supported, 1 undocumented

Static Methods

🟢 parse(urlString, parseQueryString, slashesDenoteHost)
Parses a URL string into a UrlObject (legacy API).
🟢 format(urlObject)
Formats a URL object or WHATWG URL back into a string.
🟢 resolve(from, to)
Resolves a target URL relative to a base URL (legacy API).
🟢 domainToASCII(domain)
Converts a Unicode domain name to its ASCII (Punycode) representation.
🟢 domainToUnicode(domain)
Converts an ASCII (Punycode) domain name to Unicode.
🟢 fileURLToPath(url)
Converts a file: URL to a filesystem path string.
🟢 pathToFileURL(path)
Converts an absolute filesystem path to a file: URL.
🟢 urlToHttpOptions(url)
Returns an object with URL components suitable for use with http.request().
🟢 resolveObject(source, relative)
Resolves a relative URL against a source URL object, similar to a browser resolving an anchor tag.

Properties

🟢 URL
Formats a URL object or WHATWG URL back into a string.
⚪ URLSearchParams
The WHATWG URL class (same as global URL). @compat elide Full

---

Details

parse(urlString, parseQueryString, slashesDenoteHost)

static parse(urlString: string, parseQueryString?: boolean, slashesDenoteHost?: boolean): UrlObject

static parse(urlString: string, parseQueryString?: boolean, slashesDenoteHost?: boolean): UrlObject

Parses a URL string into a UrlObject (legacy API).
ParameterTypeDescription
urlStringstringThe URL string to parse
parseQueryString (optional)booleanIf true, the query property is parsed into an object
slashesDenoteHost (optional)booleanIf true, //foo/bar treats foo as the host
Returns: UrlObject A UrlObject with the parsed URL components

> Known issue: Legacy API; prefer the WHATWG URL constructor for new code

resolve(from, to)

static resolve(from: string, to: string): string

static resolve(from: string, to: string): string

Resolves a target URL relative to a base URL (legacy API).
ParameterTypeDescription
fromstringThe base URL string
tostringThe target URL string
Returns: string The resolved URL string

> Known issue: Legacy API; prefer the WHATWG URL constructor with a base argument

fileURLToPath(url)

static fileURLToPath(url: (string or URL)): string

static fileURLToPath(url: (string or URL)): string

Converts a file: URL to a filesystem path string.
ParameterTypeDescription
url(string or URL)A file: URL string or WHATWG URL object
Returns: string The filesystem path Throws:
  • TypeError --- If the URL is not a file: URL

resolveObject(source, relative)

static resolveObject(source: (string or UrlObject), relative: string): UrlObject

static resolveObject(source: (string or UrlObject), relative: string): UrlObject

Resolves a relative URL against a source URL object, similar to a browser resolving an anchor tag.
ParameterTypeDescription
source(string or UrlObject)The base URL string or UrlObject to resolve from
relativestringThe relative URL string to resolve
Returns: UrlObject A UrlObject representing the resolved URL

> Known issue: Legacy API; prefer the WHATWG URL constructor with a base argument

Dictionary UrlObject

Structured representation of a parsed URL from the legacy url.parse() API.

Since: 1.1.0 Docs: Node.js Docs

12 members: 2 supported, 10 undocumented

Properties

🟢 protocol
Structured representation of a parsed URL from the legacy url.parse() API.
🟢 slashes
Structured representation of a parsed URL from the legacy url.parse() API.
⚪ auth
→ string?
⚪ host
→ string?
⚪ port
→ string?
⚪ hostname
→ string?
⚪ hash
→ string?
⚪ search
→ string?
⚪ query
→ string?
⚪ pathname
→ string?
⚪ path
→ string?
⚪ href
→ string?

---