
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - url.hostname 屬性
URL 模組的 URL 類中的 **NodeJS url.hostname 屬性** 允許我們獲取和設定指定 URL 的主機名部分。
**NodeJS url.hostname 屬性** 類似於 NodeJS host 屬性,但 hostname 和 host 之間的主要區別在於 hostname 不包含埠段。如果為 hostname 屬性分配任何無效的主機名值,則將忽略這些值。
語法
以下是 **URL 類 NodeJS hostname 屬性** 的語法:
URL.hostname
引數
此屬性不接受任何引數。
返回值
此屬性允許獲取和設定指定 URL 的主機名部分。
以下示例演示了路徑模組中 Node.js URL.hostname 屬性的用法。
示例
使用 NodeJS URL.hostname 屬性,我們可以獲取提供的 URL 的主機名部分。
在以下示例中,我們嘗試獲取給定 URL 的主機名部分。
const url = require('url'); const myURL = new URL('https://tutorialspoint.tw:100/index.htm'); console.log("The URL: " + myURL.href); console.log("Hostname of the URL: " + myURL.hostname);
輸出
如下所示的輸出中,URL.hostname 屬性列印給定 URL 的主機名部分。
The URL: https://tutorialspoint.tw:100/index.htm Hostname of the URL: www.tutorialspoint.com
示例
我們無法使用 URL.hostname 屬性更改 URL 中主機名部分的主機名和埠段的值。
在下面的示例中,我們嘗試使用 URL.hostname 屬性修改給定 URL 中主機名部分的主機名和埠段。
const url = require('url'); const myURL = new URL('https://tutorialspoint.tw:100/index.htm'); console.log("Before changing the port: " + myURL.href); myURL.hostname = "www.tttttttttutorialspoint.com:101" console.log("After changing hostname and the port: " + myURL.href);
輸出
如下所示的輸出中,URL.hostname 屬性對給定 URL 的主機名部分沒有任何更改。
Before changing the port: https://tutorialspoint.tw:100/index.htm After changing hostname and the port: https://tutorialspoint.tw:100/index.htm
示例
我們可以使用 URL.host 屬性設定主機名部分的主機名和埠段。
在以下示例中,我們嘗試使用 URL.host 屬性更改給定 URL 中主機名部分的主機名和埠段。
const url = require('url'); const myURL = new URL('https://tutorialspoint.tw:100/index.htm'); console.log("Before changing the hostname and port: " + myURL.href); myURL.host = "www.tutorialsssspoint.com:101" console.log("After changing the hostname and port: " + myURL.href);
輸出
如果我們編譯並執行上述程式,URL.host 屬性會對給定 URL 進行指定的修改。
Before changing the hostname and port: https://tutorialspoint.tw:100/index.htm After changing the hostname and port: https://www.tutorialsssspoint.com:101/index.htm
nodejs_url_module.htm
廣告