当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js URL.host用法及代码示例


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

用法:

const url.host

返回值:它获取并设置URL的主机部分。


以下示例程序旨在说明url.host方法的用法:

范例1:

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

输出

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

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

范例2:

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



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node | URL.host API。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。