Computer scienceBackendNode.jsCore ConceptsInternal modulesHTTP server

Intro to URL module

7 minutes read

The URL module in Node.js allows you to handle and manage URL (Uniform Resource Locator) strings. A URL is a string that defines the location of a resource or any form of content on the internet. URLs consist of several components, including the protocol, the domain name, and the path to the resource. The url module in Node.js allows developers to easily extract information from URLs. In this topic, you'll learn how to utilize the url module in Node.js.

URL module

URL is a fundamental component of the World Wide Web that specifies the location of a resource on the web and it consists of several components like the protocol, the pathname, the domain name, etc. It is widely used to identify resources such as web pages, videos, images, and many other files that are available on the internet. You can use the url module in Node.js to construct or modify URLs. It is a built-in module available in Node.js that allows you to parse and manipulate URLs.

The url module in Node.js has different ways to parse and format URLs. It is used to extract the exact information required from a URL, also you can create a URL from its component parts which you'll study in a later section. Before extracting such information and creating URLs, let's start with basic access. To access the url module in your Node.js application, use the require method to include the url module.

const url = require("node:url");

Now, you can access the url module in your application and use its various classes and functions to work with URLs.

Parsing URLs

url.parse() is a simple method that you can use to parse a URL. This method accepts a URL string as input and returns an object — precisely a URL object with the properties or attributes of the input URL. Let's look at a specific example:

const url = require("url");

const parsedUrl = url.parse("https://de.wikipedia.org/wiki/Wikipedia:Hauptseite");

console.log(parsedUrl);

This code snippet gives the following output:

Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'de.wikipedia.org',
  port: null,
  hostname: 'de.wikipedia.org',
  hash: null,
  search: null,
  query: null,
  pathname: '/wiki/Wikipedia:Hauptseite',
  path: '/wiki/Wikipedia:Hauptseite',
  href: 'https://de.wikipedia.org/wiki/Wikipedia:Hauptseite'
}

Here, the url module from Node.js is accessed first. The URL string is then defined and parsed using the url.parse() method. As seen in the output above, the method returns an object with many URL characteristics or components such as protocol, auth, hostname, hash, pathname, etc.

The url.parse() method is deprecated and it is advised to use WHATWG URL API. It is preferable to use the URL class for parsing and working with URLs instead of parse(). Here's an equivalent code snippet using the URL class:

const url = require("node:url");

const newUrl = new URL("https://de.wikipedia.org/wiki/Wikipedia:Hauptseite");

console.log(newUrl);

You will get the following output when using the URL class:

URL {
  href: 'https://de.wikipedia.org/wiki/Wikipedia:Hauptseite',
  origin: 'https://de.wikipedia.org',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'de.wikipedia.org',
  hostname: 'de.wikipedia.org',
  port: '',
  pathname: '/wiki/Wikipedia:Hauptseite',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

Comparing outputs given by the url.parse() method and the URL class, the legacy way to parse a URL also produces the same output as the new WHATWG API.

You can also access properties using the dot notation. For example parsedUrl.protocol, parsedUrl.slashes, and so on.

Constructing URLs

You can also construct your own URL using the url.format() method available in the url module in Node.js. This method takes an object containing various components of the URL and returns a formatted URL string.

Let's see how you can construct your URL string.

const url = require("url");

// Creating a URL object with various properties
const urlObject = {
  protocol: 'https:',
  hostname: 'www.dummywebsite.com',
  pathname: '/mydocument',
  auth: 'username:password',
  port: '443',
};

// Converts a URL object and returns a URL string
const urlString = url.format(urlObject);

console.log(urlString);

Here's the output of the code snippet given above:

https://username:[email protected]:443/mydocument

In the example above, an object — urlObject is defined at first which contains different properties of the URL. This object is passed on to the url.format() method to convert it into a URL formatted string as you can see in the output above.

Here's the equivalent code using the newer Node.js API:

const url = require("url");

// Creating a URL object with various properties
const urlObject = new URL("https://www.dummywebsite.com/mydocument");
urlObject.username = "username";
urlObject.password = "password";

// Converts a URL object and returns a URL string
const urlString = urlObject.toString();

console.log(urlString);

The output for this code snippet is:

https://username:[email protected]/mydocument

In the newer API of Node.js, the URL class is used instead of the url module. The URL class provides a modern and consistent way to work with URLs.

Resolving URLs

In Node.js, you can use the url.resolve() method to resolve a URL. This method accepts two URL strings as input and combines them to form a single URL.

Let's look at an example of a relative URL and a base URL to see how this works:

const url = require('url');

const baseUrl = 'https://www.abaseurl.com';
const relativeUrl = '/find?q=javascript';

// Resolves the two URLs into a single URL
const resolvedUrl = url.resolve(baseUrl, relativeUrl);

console.log(resolvedUrl);

The code snippet shown above gives the following output:

https://www.abaseurl.com/find?q=javascript

A baseUrl and a relativeUrl are defined as demonstrated in the previous example. The url.resolve() method combines these URLs into a single URL. In this example, the url.resolve() accepts two URLs and combines them into a single URL. This method is particularly helpful for dynamically creating URLs in your Node.js application.

URL constructor

The different methods discussed until now were legacy API or deprecated ways to parse and construct the URLs. This means url.parse(), url.format(), url.resolve() are deprecated in the newer versions of Node.js even though they are still widely used and accepted in large applications. However, it is recommended to use the URL constructor to parse the URLs as it is a more modern and consistent API. You can learn more about legacy URL API and WHATWG URL API and compare the differences.

Let's see how you can parse a URL using the URL constructor in the example below:

const { URL } = require("url");

// URL constructor
const firstURL = new URL("https://de.wikipedia.org/wiki/Wikipedia:Hauptseite");

console.log(firstURL);

console.log(typeof firstURL);

The code snippet gives the following output:

URL {
  href: 'https://de.wikipedia.org/wiki/Wikipedia:Hauptseite',
  origin: 'https://de.wikipedia.org',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'de.wikipedia.org',
  hostname: 'de.wikipedia.org',
  port: '',
  pathname: '/wiki/Wikipedia:Hauptseite',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}
object

As shown in the example, the URL constructor gives the same output as that of the url.parse method. Also, the URL constructor returns a URL object just like the url.parse method. You can also specify or manipulate various URL parameters as shown in the following example:

const { URL } = require("url");

const firstURL = new URL("https://de.wikipedia.org/wiki/Wikipedia:Hauptseite");

console.log(firstURL.protocol);
console.log(firstURL.pathname);
console.log(firstURL.href);
console.log(firstURL.hostname);

This piece of code gives the following output:

https:
/wiki/Wikipedia:Hauptseite
https://de.wikipedia.org/wiki/Wikipedia:Hauptseite
de.wikipedia.org

Conclusion

To sum up, let's look at the major points discussed in this topic:

  • You learned about url module and its use cases in Node.js;
  • The url module provides a way to parse, format, and resolve any URLs;
  • The url.parse() method is used to parse a URL string to a URL object;
  • Similarly, you can use url.format() if you want to create your own URL string from an object;
  • These are legacy methods and are deprecated in newer versions of Node.js but they are still popular among developers;
  • You can use a URL constructor to construct and modify the URLs.
4 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo