One data grid for JavaScript, React, Vue & Angular. Sorting, filtering, virtualization, pinning, and CSV export included. Early access $199 $349 → [Get it now]

How to work with URLs in Node.js

Working with URLs is essential for building web applications, APIs, and handling HTTP requests in Node.js. As the creator of CoreUI, a widely used open-source UI library, I’ve built countless Node.js applications that require robust URL parsing and manipulation. The most effective approach is using Node.js’s built-in URL class, which provides a complete API for parsing, constructing, and modifying URLs. This modern approach handles edge cases and follows web standards correctly.

Use the built-in URL class to parse and manipulate URLs with full support for all URL components.

const url = new URL('https://api.example.com/users?page=1&limit=10')

console.log(url.hostname) // 'api.example.com'
console.log(url.pathname) // '/users'
console.log(url.searchParams.get('page')) // '1'

The URL constructor creates a URL object that provides access to all URL components including protocol, hostname, pathname, search parameters, and hash. The searchParams property returns a URLSearchParams object that offers methods like get(), set(), append(), and delete() for working with query parameters. This approach is more reliable than manual string manipulation and handles encoding/decoding automatically.

Best Practice Note:

This is the same approach we use in CoreUI’s Node.js backend services for handling API endpoints and routing. The URL class also supports relative URLs when provided with a base URL as the second argument, making it perfect for building dynamic URLs in web applications.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author