📜  Node特殊方案| URL.protocol API

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

Node特殊方案| URL.protocol API

url.protocol是 URL 模块中 URL 类的内置应用编程接口,用于获取和设置 URL 的协议方案。

句法:

const url.protocol

返回值:获取和设置URL的协议方案

示例 1:本示例将特殊协议更改为 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);


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


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


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


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


输出:

Before Change
http://gfg.org/foo

After Change
https://gfg.org/foo

示例2:本示例尝试将非特殊协议更改为smtp->http 等特殊协议,但不会更改。

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

输出:

Before Change
smtp://gfg.org/foo

After Change
smtp://gfg.org/foo

示例 3:本示例尝试将特殊协议更改为 ftp->fish 等假设协议,但不会更改。

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

输出:

Before Change
ftp://gfg.org/foo

After Change
ftp://gfg.org/foo

示例 4:此示例尝试从非特殊协议更改为 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);

输出:

Before Change
ssh://gfg.org/foo

After Change
fish://gfg.org/foo

示例 5:它可以用作吸气剂。

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

输出:

https:

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