The Node.js URL module helps us create, read, and modify URLs. We can use it to access the protocol, hostname, pathname, query parameters, and other parts of a URL.
URL class for working with URLs.
URL stands for Uniform Resource Locator. It identifies the location of a resource and may include query parameters or a fragment.
https://www.example.com:8080/products?id=25&category=books#details
| Part | Value | Meaning |
|---|---|---|
| Protocol | https: |
Communication protocol. |
| Hostname | www.example.com |
Host or domain name. |
| Port | 8080 |
Optional network port. |
| Pathname | /products |
Path of the requested resource. |
| Query string | ?id=25&category=books |
Additional key-value parameters. |
| Fragment | #details |
Reference to a section or location. |
Modern Node.js provides the global URL class.
We can create a URL object by passing a valid absolute URL.
const myURL = new URL(
'https://www.example.com/products?id=25'
);
console.log(myURL.href);
console.log(myURL.hostname);
console.log(myURL.pathname);
https://www.example.com/products?id=25
www.example.com
/products
The URL object parses the address into separate properties that we can access in our program.
| Property | Purpose |
|---|---|
href |
Returns the complete serialized URL. |
protocol |
Returns the protocol, such as https:. |
hostname |
Returns the hostname. |
port |
Returns the explicit port, if present. |
pathname |
Returns the path of the URL. |
search |
Returns the query string, including ?. |
hash |
Returns the fragment, including #. |
origin |
Returns the scheme, hostname, and applicable port. |
const myURL = new URL(
'https://example.com:8080/student/profile?id=10#info'
);
console.log('Protocol:', myURL.protocol);
console.log('Hostname:', myURL.hostname);
console.log('Port:', myURL.port);
console.log('Pathname:', myURL.pathname);
console.log('Search:', myURL.search);
console.log('Hash:', myURL.hash);
console.log('Origin:', myURL.origin);
Protocol: https:
Hostname: example.com
Port: 8080
Pathname: /student/profile
Search: ?id=10
Hash: #info
Origin: https://example.com:8080
Query parameters are values included after the question mark
(?) in a URL. The searchParams property
lets us read, add, update, and delete these parameters.
const myURL = new URL(
'https://example.com/student?id=101&name=Rahul'
);
console.log(myURL.searchParams.get('id'));
console.log(myURL.searchParams.get('name'));
101
Rahul
The get() method returns the value associated
with the requested parameter, or null if
the parameter does not exist.
const myURL = new URL('https://example.com/courses');
myURL.searchParams.set('course', 'Node.js');
myURL.searchParams.set('duration', '6 months');
console.log(myURL.href);
https://example.com/courses?course=Node.js&duration=6+months
The set() method adds a parameter or replaces
its existing value.
const myURL = new URL(
'https://example.com/products?id=25&category=books'
);
myURL.searchParams.delete('category');
console.log(myURL.href);
https://example.com/products?id=25
The delete() method removes the specified
query parameter.
The URL class is available globally, but we can also import
URL-related utilities from the built-in url module.
const { URL } = require('url');
const myURL = new URL('https://example.com/about');
console.log(myURL.pathname);
/about
Both the global URL class and the imported
URL class provide the standard URL API.
If we want to create a URL from a relative path, we must provide a base URL.
const myURL = new URL(
'/courses/nodejs',
'https://example.com'
);
console.log(myURL.href);
console.log(myURL.pathname);
https://example.com/courses/nodejs
/courses/nodejs
TypeError.
Suppose we want to create a URL for searching student records. We can add the student's ID and name as query parameters.
const myURL = new URL('https://example.com/students');
myURL.searchParams.set('studentId', 'ST101');
myURL.searchParams.set('name', 'Amit Kumar');
myURL.searchParams.set('course', 'Full Stack Development');
console.log('Complete URL:', myURL.href);
console.log('Student ID:', myURL.searchParams.get('studentId'));
console.log('Student Name:', myURL.searchParams.get('name'));
console.log('Course:', myURL.searchParams.get('course'));
Complete URL: https://example.com/students?studentId=ST101&name=Amit+Kumar&course=Full+Stack+Development
Student ID: ST101
Student Name: Amit Kumar
Course: Full Stack Development
Save the code as url_example.js and run:
node url_example.js.
| Method / Property | Purpose |
|---|---|
new URL(input, base) |
Creates and parses a URL. |
url.href |
Returns the complete URL. |
url.hostname |
Returns the hostname. |
url.pathname |
Returns the URL path. |
url.searchParams.get() |
Reads a query parameter. |
url.searchParams.set() |
Adds or updates a query parameter. |
url.searchParams.delete() |
Removes a query parameter. |
url.searchParams.has() |
Checks whether a parameter exists. |
https://soopropathshala.com/students.searchParams.get().Challenge: Create a URL using a relative path and a base URL, then display the final URL.
Select the correct answer for each question.
1. What does URL stand for?
2. Which property gives the pathname of a URL?
3. Which method reads a query parameter?