The Node.js URL module provides utilities for URL resolution and parsing, allowing developers to create, manipulate, and encode URLs. This module is a part of Node.js' core modules, so you don't need to install it separately with npm.
Node.js URL module has two primary components:
const url = require('url');
The URL constructor creates and returns a URL object, representing the URL defined by the parameters.
const myURL = new url.URL('https://example.com:8080/pathname?search=test#hash');
Here, myURL represents a URL object which can be manipulated and read for its constituent parts, such as protocol, host, path, search parameters, etc.
const myURL = new url.URL('https://example.com:8080/pathname?search=test#hash');
console.log(myURL.protocol); // 'https:'
console.log(myURL.hostname); // 'example.com'
console.log(myURL.port); // '8080'
console.log(myURL.pathname); // '/pathname'
console.log(myURL.search); // '?search=test'
console.log(myURL.hash); // '#hash'
The URLSearchParams interface provides utility methods to work with the query string of a URL. You can perform operations like reading, adding, deleting parameters from the query string.
const params = new url.URLSearchParams('user=abc&book=nodejs');
console.log(params.has('user')); // true
console.log(params.get('book')); // 'nodejs'
params.append('author', 'john');
console.log(params.toString()); // 'user=abc&book=nodejs&author=john'
The URL module provides a method called resolve to resolve a target URL relative to a base URL as web browsers do.
const base = 'http://example.com/dir/';
const relative = '../otherdir/';
console.log(url.resolve(base, relative)); // 'http://example.com/otherdir/'
The format method of the URL module can be used to format a URL object into a URL string.
const myURL = new url.URL('https://example.com/pathname?search=test#hash');
console.log(url.format(myURL)); // 'https://example.com/pathname?search=test#hash'
Now, let’s develop a more detailed example using Express.js with Node.js and URL module. We will be creating a simple server that can parse incoming URLs and return information about them.
Create a new directory and run the following commands:
bash
npm init -y
npm install express
Create an `index.js` file and write the following code:
const express = require('express');
const url = require('url');
const app = express();
const port = 3000;
app.get('*', (req, res) => {
const myURL = new url.URL(req.protocol + '://' + req.get('host') + req.originalUrl);
let urlInfo = {
href: myURL.href,
protocol: myURL.protocol,
hostname: myURL.hostname,
port: myURL.port,
pathname: myURL.pathname,
search: myURL.search,
hash: myURL.hash,
};
res.json(urlInfo);
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Here, we are creating a basic Express server that listens to all incoming GET requests, parses the incoming URL, and sends back a JSON object containing the URL's components.
Run the server using:
bash
node index.js
Visit `http://localhost:3000/test/path?param1=value1#anchor` in a web browser or through a tool like `curl`, and you will receive a JSON response containing information about the parsed URL.
The URL module in Node.js is an essential tool for working with URLs, allowing developers to parse, resolve, and format URLs with ease. By integrating this module with Express or any other Node.js frameworks, you can easily handle URL manipulations and obtain various components of the URLs, enhancing the web development experience.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here