Standard library / url · on the one-page reference
url
Stable — pure string parsing (the same underlying URI parser used elsewhere for HTTP requests), no syscalls, works identically on the native and WebAssembly targets.
A practical subset of
Node's url
module: the older, function-based url.parse()/
format() API, not the class-based URL object.
protocol keeps its trailing colon, search/
hash keep their leading ?/#, matching
WHATWG conventions; unlike path.parse/format,
parsing a malformed URL doesn't error, it falls back to an empty record
(pathname stays "/", href always
keeps the original input verbatim).
url.parse(str)
string → { protocol, hostname, port, pathname, search, hash, href, query }
— the second record-returning builtin family after
path.parse, same pattern: a synthetic record type, all
fields plain (non-optional) strings, except query.
query is search (which stays the raw
"?a=1&b=2" string, unchanged) additionally parsed
into a real Map<string, string> — both are
available, not a choice between them. A repeated key overwrites (last
one wins), the same behavior any repeated Map.set() call
already has.
const u = url.parse("https://example.com:8080/foo?a=1&b=hello#c");
console.log(u.hostname); // example.com
console.log(u.port); // 8080
console.log(u.pathname); // /foo
console.log(u.search); // ?a=1&b=hello
console.log(u.hash); // #c
console.log(u.query.get("a")); // 1
console.log(u.query.get("b")); // hello
url.format(parts)
{ protocol, hostname, port, pathname, search, hash, href } → string
— reconstructs a URL string from the record; round-trips with
parse.
const u = url.parse("https://example.com/foo?a=1");
console.log(url.format(u)); // https://example.com/foo?a=1