URL Module In Node.js

Introduction
 
As nodejs.org suggests:
 
The URL module provides utilities for URL resolution and parsing. It can be accessed using: 
  1. var url = require('url');  
Url module is one of the core modules that comes with node.js, which is used to parse the URL and its other properties. 
 
By using URL module, it provides us with so many properties to work with.
 
These all are listed below:
 
 
 
As you can see in the above screen, there are various properties used for URL module.
 
Below is the snippet to check the URL properties with URL : localhost:4200.
 
MyApp.js 
  1. var http = require('http');  
  2. var url = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     var queryString = url.parse(req.url, true);  
  7.     console.log(queryString);  
  8.     
  9. }).listen(4200);  
Now run the above snippet using node MyApp.js, and you can see the console like this:
 
 
 
Because we do not have a path attached to URL, now I'm writing text within my URL like below:
  1. http://localhost:4200/manav  
And again I compiled the snippet and got output like this:
 
 
 
So now, I got my complete pathname along with href as well as the path name and other properties with null values. 
 
href
 
Href property returns the complete URL along with all search terms and other information as well. 
 
href.js 
  1. var http = require('http');  
  2. var url = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     // Parsing url   
  6.     var queryString = url.parse(req.url,true);  
  7.     // Accessing href property of an URL  
  8.     console.log("Complete href is :-"+queryString.href);  
  9.     
  10. }).listen(4200);  
Execute the above snippet by writing node url.js and you can see the console like this:
 
 
 
If we change our URL to www.customway.com/abc.html:
  1. var http = require('http');  
  2. var url = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     var queryString = url.parse(req.url,true);  
  7.     queryString.href = "www.customway.com/abc.html";  
  8.     console.log("Complete href is :-"+queryString.href);  
  9.     
  10. }).listen(4200);  
And after that, you may get href like :
 
 
 
So in this way we can get href from the requested URL
 
host  and hostname
 
Host property of a URL module provides the host associated with URL.
 
host.js 
  1. var http = require('http');  
  2. const { URL } = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     var queryString = url.parse(req.url,true);  
  7.     // Prints the host  
  8.     console.log("Host is :-"+queryString.host);  
  9.   
  10.     // Prints the host name   
  11.     console.log("Host name is :-"+queryString.hostname);  
  12.     
  13. }).listen(4200);  
After executing the above snippet you may get output like this: 
 
 
 
What is the difference between host and hostname?
 
There is one major difference between both of them, that is that host includes the port name along with hostname, where hostname property does not include the port number.  
 
Now I'm going to create a new URL using the below snippet:
  1. var http = require('http');  
  2. const { URL } = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     const queryString = new URL('https://www.customway.com:11/c#corner');  
  7.     console.log("Host is :-"+queryString.host);  
  8.     console.log("Host name is :-"+queryString.hostname);  
  9.     
  10. }).listen(4200);  
So now, I have specified custom URL with port number 11, let's see the difference :
 
 
 
The host includes the port number, whereas hostname does not contain port number along with URL.
 
Pathname and Searchparam
 
Path and pathname is the combination of pathname with URL and also contains a search term along with the URL
 
Let's say we have URL with multiple search parameters like :
  • www.demo.com/test1/test2/test3?qstring=value
In above url:
  • path =  /test1/test2/test3
  • Search param =  qstring=value
In the same way we can get this via URL module property; find the snippet below :
 
path.js 
  1. var http = require('http');  
  2. const { URL } = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     const queryString = new URL('https://www.customway.com/test/test1/test2/test3?username=manav');  
  7.     console.log("Path name is :-"+queryString.pathname);  
  8.     console.log("Search Parameter is :-"+queryString.searchParams);  
  9.     
  10. }).listen(4200);  
Now execute it by writing node path.js, and you will get output like this:
 
 
 
This way you can access the complete path along with search parameters.
 
Search
 
Search property is the same as searchParams that we have seen before 
 
But the main difference is search property includes [?] along with all search parameters 
 
search.js
  1. var http = require('http');  
  2. const { URL } = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     const queryString = new URL('https://www.customway.com?username=manav&password=123');  
  7.     console.log("Search terms are :-"+queryString.search);  
  8.     
  9. }).listen(4200);  
Now you will be able to get the whole search term along with [?] symbol attached :
 
 
 
As you can see both of my search terms were included along with [?] symbol
 
Port
 
Port property of a URL module returns the port associated with a URL.
 
port.js 
  1. var http = require('http');  
  2. const { URL } = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     const queryString = new URL('https://www.customway.com:4200');  
  7.     console.log("Port is :-"+queryString.port);  
  8.     
  9. }).listen(4200);  
I've used port number 4200 along with URL, and may get output like this:
 
 
 
Now if I want to change port number, than we can do like this:
  1. const queryString = new URL('https://www.customway.com:4200');  
  2.   queryString.port = '4500'// changed port number to 4500  
  3.   console.log("Port is :-"+queryString.port);  
And probably you will get output in the console:
  • Port is :- 4500 
Protocol
 
Protocol property is used to get specific protocols used for any request.

protocol.js 
  1. var http = require('http');  
  2. const { URL } = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     const queryString = new URL('https://www.customway.com');  
  7.     console.log("Protocol used :-"+ queryString.protocol);  
  8.     
  9. }).listen(4200);  
Now, you will get the protocol name for what you have requested :
 
 
 
Hash
 
Hash property of a URL returns decorated with [#] sybmol.
 
Sometimes, we have pages with multiple div, and we have provided ids along with #name, so in a node, we can also access hash fragment portion attached to URL.
 
hash.js
  1. var http = require('http');  
  2. const { URL } = require('url');  
  3.   
  4. http.createServer(function (req, res) {  
  5.     
  6.     const queryString = new URL('https://www.customway.com#manav');  
  7.     console.log("Hash Fragment Is :-"+ queryString.hash);  
  8.     
  9. }).listen(4200);  
After executing node hash.js you will get output like :
 
 
 
Conclusion
 
So far we have covered many properties of a URL module which are:
  • href
  • host
  • hostname
  • pathname
  • queryParams
  • port
  • protocol
  • hash 
By using these properties you can perform plenty of operations with your node.js application specific to URL.
 
To follow my other node.js articles you can go through the following links:
I hope you have learned something from this article, Thanks for reading.


Similar Articles