Special Schemes of Node.js URL.protocol API
Last Updated :
14 Oct, 2021
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.
Javascript
const http = require( 'url' );
console.log( "Before Change" );
console.log(myURL.href);
console.log();
myURL.protocol = 'https' ;
console.log( "After Change" );
console.log(myURL.href);
|
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.
Javascript
const http = require( 'url' );
console.log( "Before Change" );
console.log(myURL.href);
console.log();
myURL.protocol = 'http' ;
console.log( "After Change" );
console.log(myURL.href);
|
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.
Javascript
const http = require( 'url' );
console.log( "Before Change" );
console.log(myURL.href);
console.log();
myURL.protocol = 'fish' ;
console.log( "After Change" );
console.log(myURL.href);
|
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.
Javascript
const http = require( 'url' );
console.log( "Before Change" );
console.log(myURL.href);
console.log();
myURL.protocol = 'fish' ;
console.log( "After Change" );
console.log(myURL.href);
|
Output:
Before Change
ssh://gfg.org/foo
After Change
fish://gfg.org/foo
Example 5: It can be used as a getter.
Javascript
const http = require( 'url' );
const protocol = myURL.protocol;
console.log(protocol);
|
Output:
https:
Reference: https://developer.mozilla.org/en-US/docs/Web/API/URL/protocol
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...