📜  节点 | URL.protocol API

📅  最后修改于: 2022-05-13 01:56:57.112000             🧑  作者: Mango

节点 | URL.protocol API

url.protocol是 url模块中类 URL的内置应用程序编程接口,用于获取和设置 URL 的协议部分。当使用其中一种特殊协议解析 URL 时,url.protocol 属性可能会更改为另一种特殊协议,但不能更改为非特殊协议,反之亦然。

句法:

const url.protocol

返回值:它返回 URL 的协议部分。

下面的例子说明了url.protocol方法在 Node.js 中的使用:

示例 1:



Javascript
// 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);
   
// 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);


Javascript
// 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);
   
// Assigning 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);


Javascript
// 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);


输出

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

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

示例2:本示例将特殊协议更改为非特殊协议。

Javascript

// 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);
   
// Assigning 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);

输出

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

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

示例 3:

Javascript

// 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);

输出:

Protocol of current url is : https:

注意:上述程序将使用node myapp.js命令编译运行。
参考: https://nodejs.org/api/url.html#url_url_protocol