The URL module in Node.js is used when working with URLs in our Node.js file. It provides functions to access different parts of our URL. We use this module in web development when we work with URLs to make HTTP requests to external APIs and handle file paths.
To use the URL module, we don't have to install any additional packages. It is in-built in Node. We only include it in our Node.js file using the require
statement.
const url = require('url');
We can parse our URL using the URL module to study its various components. We can easily access and manipulate these individual components for various purposes, such as data extraction, URL rewriting, or performing conditional logic based on specific parts of the URL.
The syntax of the parse()
function is as follows:
const parsedUrl = url.parse(urlString, parseQueryString, slashesDenoteHost);
urlString
: The URL string that we want to parse.
parseQueryString
(optional): The boolean value that determines whether we want the query string as an object or string. If we set the value to true
, the query string acts as an object with key-value pairs. Otherwise, it just acts as a string. By default, its value is false
.
slashesDenoteHost
(optional): The boolean value that controls the interpretation of double slashes (//
) in our URL string. If we set the value to true
, the text between //
and the next /
is treated as the host. Otherwise, the text between //
and the next /
is considered to be a part of the path name. By default, its value is false
.
Note: The parse() method has been deprecated after node version 11.0.0, but is still available and functional for use in code. However, the Node.js documentation advises us to use the
URL
class as a preferred alternative for URL parsing and manipulation.
The following example will help us better understand the components of a URL:
Protocol: The communication protocol used by our URL.
Hostname: The domain name of our network.
Port: The port number we use to establish the network connection.
Host: The combination of the hostname and the port.
Path: The location that the URL focuses on.
Search: The information we pass to the server.
Query: The key-value pairs within our URL's search component.
Hash: The specific section in our URL that comes after the # symbol.
Let's understand all the components we can access in our URL using a coding example.
const url = require('url');const urlString = 'https://educative.com:8080/products?page=1&sort=desc#section1';const parsedURL = url.parse(urlString, true);console.log('Protocol:', parsedURL.protocol);console.log('Host:', parsedURL.host);console.log('Hostname:', parsedURL.hostname);console.log('Port:', parsedURL.port);console.log('Path:', parsedURL.path);console.log('Search:', parsedURL.search);console.log('Query:', parsedURL.query);console.log('Hash:', parsedURL.hash);
Line 1: We import the url
module from Node.js.
Line 3: We declare a variable urlString
which contains our URL.
Line 4: We use the url.parse()
method to parse the URL string into its components. The second parameter true
indicates that the query
property should be parsed into an object so we can access its components independently.
Lines 6–13: We print all the components of our URL on the console.
The URL module in Node.js provides us with multiple functions to work with URLs. Its parse
method allows us to manipulate our URLs, making it easier for us to access its components for various purposes.
Free Resources