Special Schemes of Node.js 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.
Javascript
// 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); // Assigning protocol portion // using protocol console.log(); myURL.protocol = 'https' ; // Display href value of myURL after change 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
// 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); // Assigning protocol portion // using protocol console.log(); myURL.protocol = 'http' ; // Display href value of myURL after change 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
// 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); // Assigning protocol portion // using protocol console.log(); myURL.protocol = 'fish' ; // Display href value of myURL after change 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
// 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); // Assigning protocol portion // using protocol console.log(); myURL.protocol = 'fish' ; // Display href value of myURL after change 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
// 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); |
Output:
https:
Reference: https://developer.mozilla.org/en-US/docs/Web/API/URL/protocol