Node.js “url” Module With Examples

The Node.js url module, which provides utilities for working with Uniform Resource Locators (URLs).

In this tutorial, we’ll explore the functionalities of the url module, understand how it aids developers in handling URLs, and demonstrate practical use cases.

Introducing the “url” Module

The url module in Node.js simplifies URL parsing, formatting, and resolution tasks. To leverage its capabilities, begin by including the module in your script:

const url = require('url');

Now, let’s delve into the key functionalities offered by the url module.

1. Parsing URLs: “url.parse()”

The url.parse() method is fundamental for dissecting a URL into its constituent parts. It returns an object containing properties such as protocol, hostname, pathname, and more.

const url = require('url');
const urlString = '';
const parsedUrl = url.parse(urlString, true);

console.log('Parsed URL:', parsedUrl);
Parsed URL: Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: '',    
  port: null,
  hostname: '',
  hash: null,
  search: '?query=parameter',
  query: [Object: null prototype] { query: 'parameter' },
  pathname: '/path',
  path: '/path?query=parameter',
  href: ''

The true argument in the example instructs the parser to populate the query property with an object representing the URL’s query parameters.

2. Formatting URLs: “url.format()”

Conversely, the url.format() method transforms a parsed URL object back into a formatted URL string. This is useful when you need to construct URLs dynamically.

const url = require('url');
const formattedUrl = url.format({
    protocol: 'https',
    host: '',
    pathname: '/path',
    query: { query: 'parameter' },

console.log('Formatted URL:', formattedUrl);
// Output: Formatted URL:

3. Resolving URLs: “url.resolve()”

When working with relative URLs, the url.resolve() method proves handy. It resolves a target URL relative to a base URL, providing a complete and absolute URL.

const url = require('url');
const baseUrl = '';
const relativePath = '/path';
const resolvedUrl = url.resolve(baseUrl, relativePath);

console.log('Resolved URL:', resolvedUrl);
// Output: Resolved URL:

Practical Use Cases

Parsing Query Parameters

Parsing query parameters from a URL is a common task. With the url.parse() method, you can easily extract and work with query parameters.

const url = require('url');
const queryString = '';
const parsedUrl = url.parse(queryString, true);

console.log('Age:', parsedUrl.query.age);
 * Output:
 * Name: John
 * Age: 30 

Building Dynamic URLs in Web Servers

In a web server scenario, you might construct dynamic URLs based on user input or application logic. The url.format() method facilitates this process, ensuring the correct formatting of URLs.

const http = require('http');
const url = require('url');

const server = http.createServer((req, res) => {
    const parsedUrl = url.parse(req.url, true);
    // Process the parsed URL and generate a response
    res.end('Hello, Node.js!');

server.listen(3000, () => {
    console.log('Server listening on port 3000');