URL & QueryString in Node.js
From the Node.js Core Modules cheat sheet ยท Utilities & Advanced ยท verified Jul 2026
URL & QueryString
Parse and construct URLs and query strings
javascript
const { URL } = require('url')
const querystring = require('querystring')
// Modern URL API (recommended)
const myUrl = new URL('https://example.com:8000/path?name=John&age=30')
console.log({
hostname: myUrl.hostname,
pathname: myUrl.pathname,
searchParams: myUrl.searchParams.get('name')
})
// Modify URL
myUrl.searchParams.append('role', 'admin')
myUrl.pathname = '/users/profile'
console.log(myUrl.href)
// Parse query string
const query = querystring.parse('name=John&age=30&hobbies=coding&hobbies=music')
// Result: { name: 'John', age: '30', hobbies: ['coding', 'music'] }๐ข Essential - URL parsing for web applications
๐ก Use new URL() instead of legacy url.parse()
๐ URLSearchParams for easy query manipulation
โก URL validation happens in constructor
๐ Related: path module for file paths
urlutilities