📜  Node.js URL.host API

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

Node.js URL.host API

url.host是类URL的内置应用程序编程接口,带有 in url模块,用于获取和设置 URL 的主机部分。

句法:

const url.host

返回值:获取和设置 URL 的主机部分。
下面的程序说明了使用url.host方法:
示例 1:

javascript
// node program to demonstrate the 
// url.host API as Setter 
   
//importing the module 'url'
const http = require('url');
   
// creating and initializing myURL
const myURL = new URL('https://example.com:80/foo#ram');
   
// Display href value of myURL before change
console.log("Before Change");
console.log(myURL.href);
   
// assigning host portion
// using host
console.log();
myURL.host = 'example.com:82';
   
// Display href value of myURL after change
console.log("After Change");
console.log(myURL.href);


javascript
// node program to demonstrate the 
// url.host API as Getter 
  
//importing the module 'url'
const http = require('url');
  
// creating and initializing myURL
const myURL = new URL('https://example.org:82/foo#ram');
  
// getting the host portion
// using host
const host = myURL.host;
  
// Display hash value 
console.log(host);


输出

Before Change
https://example.com:80/foo#ram

After Change
https://example.com:82/foo#ram

示例 2:

javascript

// node program to demonstrate the 
// url.host API as Getter 
  
//importing the module 'url'
const http = require('url');
  
// creating and initializing myURL
const myURL = new URL('https://example.org:82/foo#ram');
  
// getting the host portion
// using host
const host = myURL.host;
  
// Display hash value 
console.log(host);

输出:

example.org:82

注意:上述程序将使用node myapp.js命令编译和运行。
参考:
https://nodejs.org/dist/latest-v10.x/docs/api/url.html#url_url_host