jsrosetta

Standard library

URL Parsing

Node.js's WHATWG URL compared to Go's net/url, Rust's url crate, Swift's URLComponents, and Java's java.net.URI: getting the scheme, user info, port, path, and query.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.63Swift ≥ 4.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

All five languages parse a URL into its separate parts: scheme, user info, host, port, path, query. Node.js uses the global URL class (the WHATWG standard); Go returns a *url.URL struct with corresponding fields and methods. Rust has no URL parser in std, so it uses the url crate (from the Servo project, also WHATWG-compliant like Node.js). Swift uses Foundation's URLComponents. Java uses the built-in java.net.URI — but unlike the other four, it has no built-in function to turn the query string into a map.

Splitting a URL into parts

const urlstr = 'http://bob:secret@sub.example.com:8080/somepath?foo=bar'
 
const parsed = new URL(urlstr)
console.log(parsed.protocol) // http:
console.log(`${parsed.username}:${parsed.password}`) // bob:secret
console.log(parsed.port) // 8080
console.log(parsed.hostname) // sub.example.com
console.log(parsed.pathname) // /somepath
console.log(Object.fromEntries(parsed.searchParams)) // { foo: 'bar' }

Reference: github.com/miguelmota/golang-for-nodejs-developers#url-parse