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

More Node.js tasks

Back to the full Node.js Core Modules cheat sheet