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.
// 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 // 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); |
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.
// 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 // 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); |
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.
// 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 // 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); |
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.
// 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 // 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); |
Output:
Before Change ssh://gfg.org/foo After Change fish://gfg.org/foo
Example 5: It can be used as a getter.
// Node program to demonstrate the // url.pathname API as Getter // Importing the module 'url' const http = require('url'); // Creating and initializing myURL // 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
Recommended Posts:
- Different types of Coding Schemes to represent data
- Node.js | NPM (Node Package Manager)
- How to convert special characters to HTML in Javascript?
- Replace special characters in a string with underscore (_) in JavaScript
- Node js | OS
- Why Node.js ?
- PHP vs. Node.js
- Node | URL.protocol API
- Node | URL.hash API
- Node | URL.resolve(from,to) API
- Node | URL.domainToUnicode
- Node.js Events
- Node | URLSearchParams.get()
- Node | URL.domainToASCII
- Essence of Node.js
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.



