• Node.js Video Tutorials

NodeJS - url.hash 屬性



URL 類的NodeJS url.host 屬性用於獲取和設定 URL 的主機部分。如果為 host 屬性分配任何無效的主機值,則會忽略這些值。

Node.js URL 模組提供了多個用於 URL 解析和分析的實用程式。

語法

以下是NodeJS URL 類 host 屬性的語法

URL.host

引數

此屬性不接受任何引數。

返回值

此屬性獲取並設定提供的 URL 的主機部分。

示例

我們可以使用 NodeJS URL.host 屬性獲取 URL 主機部分的值。

在下面的示例中,我們嘗試獲取提供的 URL 的主機部分。

const url = require('url');

const myURL = new URL('https://tutorialspoint.tw/index.htm');
console.log(myURL.href);
console.log("Host portion of the URL: " + myURL.host);

輸出

執行上述程式後,myURL.host 屬性返回 URL 主機部分的值。

https://tutorialspoint.tw/index.htm
Host portion of the URL: www.tutorialspoint.com

示例

我們還可以使用 URL.host 屬性設定 URL 主機部分的值。

在下面的程式中,我們嘗試為給定 URL 的主機部分設定一個新值。

const url = require('url');

const myURL = new URL('https://tutorialspoint.tw/index.htm');
console.log(myURL.href);
console.log("The current host portion in URL: " + myURL.host);

myURL.host = "www.tutorix.com";
console.log("After modifying the host portion to: " + myURL.host);
console.log(myURL.href);

輸出

正如我們在下面的輸出中看到的,myURL.host 修改了給定 URL 的 fragment 屬性。

https://tutorialspoint.tw/index.htm
The current host portion in URL: www.tutorialspoint.com

After modifying the host portion to: www.tutorix.com
https://www.tutorix.com/index.htm
nodejs_url_module.htm
廣告
© . All rights reserved.