CoreUI ONE — 250+ PRO components and 25+ admin templates for React, Angular, Vue, Bootstrap and Next.js, plus Data Grid, Scheduler, 4,300+ PRO icons and the Next.js boilerplate. One license. → See what you get

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

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to get the last element in an array in JavaScript
How to get the last element in an array in JavaScript

How to compare dates with JavaScript
How to compare dates with JavaScript

How to disable a button in JavaScript
How to disable a button in JavaScript

How to check if a key exists in JavaScript object?
How to check if a key exists in JavaScript object?