Special Schemes of 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 scheme of the URL.

Syntax:

const url.protocol

Return value: It get and set protocol scheme of the URL

Example 1: This example changes the special protocols to hypothetical protocols like http->https.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Before Change
http://gfg.org/foo

After Change
https://gfg.org/foo

Example 2: This example try changes the non-special protocol to a special protocol like smtp->http but it will not change.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the  
// url.protocol API as Setter  
// Changing the protocols to special 
// protocols like smtp->http
       
// Importing the module 'url' 
const http = require('url'); 
       
// Creating and initializing myURL 
const myURL = new URL('smtp://gfg.org/foo'); 
       
// 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
smtp://gfg.org/foo

After Change
smtp://gfg.org/foo

Example 3: This example try to change the special protocols to hypothetical protocols like ftp->fish but it will not change.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the  
// url.protocol API as Setter  
// Changing of protocols to special
// protocols like ftp->fish
       
// Importing the module 'url' 
const http = require('url'); 
       
// Creating and initializing myURL 
const myURL = new URL('ftp://gfg.org/foo'); 
       
// Display href value of myURL before change 
console.log("Before Change"); 
console.log(myURL.href); 
       
// Assinging protocol portion 
// using protocol 
console.log(); 
myURL.protocol = 'fish'
       
// Display href value of myURL after change 
console.log("After Change"); 
console.log(myURL.href); 

chevron_right


Output:

Before Change
ftp://gfg.org/foo

After Change
ftp://gfg.org/foo

Example 4: This example try to change from non-special protocols to hypothetical protocols like ssh->fish.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the  
// url.protocol API as Setter  
// Changing the protocols to special
// protocols like ssh->fish
       
// Importing the module 'url' 
const http = require('url'); 
       
// Creating and initializing myURL 
const myURL = new URL('ssh://gfg.org/foo'); 
       
// Display href value of myURL before change 
console.log("Before Change"); 
console.log(myURL.href); 
       
// Assinging protocol portion 
// using protocol 
console.log(); 
myURL.protocol = 'fish'
       
// Display href value of myURL after change 
console.log("After Change"); 
console.log(myURL.href); 

chevron_right


Output:

Before Change
ssh://gfg.org/foo

After Change
fish://gfg.org/foo

Example 5: It can be used as a getter.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the 
// url.pathname API as Getter 
   
// Importing the module 'url' 
const http = require('url'); 
   
// Creating and initializing myURL 
const myURL = new URL('https://gfg.org/foo'); 
   
// Getting the path portion 
// using pathname 
const protocol = myURL.protocol; 
   
// Display path value 
console.log(protocol); 

chevron_right


Output:

https:

Reference: https://developer.mozilla.org/en-US/docs/Web/API/URL/protocol



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

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.




Article Tags :

Be the First to upvote.


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