Node | URL.protocol API

The url.protocol is an inbuilt application programming interface of class URL within url module which is used to get and set the protocol portion of the URL. When a URL is parsed using one of the special protocols, the url.protocol property may be changed to another special protocol but cannot be changed to a non-special protocol, and vice versa.

Syntax:

const url.protocol

Return value: It returns the protocol portion of the URL.



Below examples illustrate the use of url.protocol method in Node.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the 
// url.protocol API as Setter 
    
// Importing the module 'url'
const http = require('url');
    
// Creating and initializing myURL
const myURL = new URL('https://geeksforgeeks.org:80/foo#ram');
    
// Display href value of myURL before change
console.log("Before Change");
console.log(myURL.href);
    
// Assinging protocol portion
// using protocol
console.log();
myURL.protocol = 'http';
    
// Display href value of myURL after change
console.log("After Change");
console.log(myURL.href);

chevron_right


Output:

Before Change
https://geeksforgeeks.org:80/foo#ram

After Change
http://geeksforgeeks.org/foo#ram

Example 2: This example changes the special protocol to a non-special protocol.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the 
// url.protocol API as Setter 
    
// Importing the module 'url'
const http = require('url');
    
// Creating and initializing myURL
const myURL = new URL('https://geeksforgeeks.org:80/foo#ram');
    
// Display href value of myURL before change
console.log("Before Change");
console.log(myURL.href);
    
// Assinging protocol portion
// with non special protocol 
// using protocol
console.log();
myURL.protocol = 'xyz';
    
// Display href value of myURL after change
console.log("After Change");
console.log(myURL.href);

chevron_right


Output:

Before Change
https://geeksforgeeks.org:80/foo#ram

After Change
https://geeksforgeeks.org:80/foo#ram

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the 
// url.protocol API as Getter 
   
// Importing the module 'url'
const http = require('url');
   
// Creating and initializing myURL
const myURL = new URL('https://geeksforgeeks.org:80/foo#ram');
   
// Getting the protocol portion
// using protocol
const protocol = myURL.protocol;
   
// Display hash value 
console.log("Protocol of current url is : " + protocol);

chevron_right


Output:

Protocol of current url is : https:

Note: The above program will compile and run by using the node myapp.js command.

Reference: https://nodejs.org/api/url.html#url_url_protocol



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.