• Node.js Video Tutorials

NodeJS - url.username 屬性



URL 類 的 **NodeJS url.username 屬性** 用於獲取和設定提供的 URL 的使用者名稱。如果無效的 URL 字元出現在分配給 username 屬性的值中,則它們將被百分比編碼。百分比編碼字元的選擇可能與 url.parse() 和 url.format() 方法產生的結果略有不同。

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

語法

以下是 **URL 類 的 NodeJS username 屬性** 的語法

URL.username

引數

此方法不接受任何引數。

返回值

此屬性獲取並設定 URL 的使用者名稱部分。

示例

如果我們將完整的 URL 分配給 NodeJS url.username 屬性,它將獲取 URL 的使用者名稱部分。

在以下示例中,我們嘗試獲取提供的 URL 的使用者名稱段。

const http = require('url');

const myURL = new URL('https://Nikhil:hyd@tutorialspoint.com');
console.log("Username portion of the URL is: " + myURL.username);

輸出

執行上述程式後,username 屬性從提供的 URL 獲取使用者名稱段。

Username portion of the URL is: Nikhil

示例

username 屬性允許將有效值設定為給定 URL 中的使用者名稱段。

在下面的程式中,我們嘗試將值設定為輸入 URL 中的使用者名稱段。

const http = require('url');

const myURL = new URL('https://Nikhil:hyd@tutorialspoint.com');
console.log("The URL: " + myURL.href);
console.log("Username portion of the URL is: " + myURL.username);

myURL.username = "Nikhilesh";
console.log("Trying to change the username to - " + myURL.username);
console.log("The URL after modifying: " + myURL.href);

輸出

正如我們在下面的輸出中看到的,輸入 URL 的使用者名稱段已修改。

The URL: https://Nikhil:hyd@tutorialspoint.com/
Username portion of the URL is: Nikhil

Trying to change the username to - Nikhilesh
The URL after modifying: https://Nikhilesh:hyd@tutorialspoint.com/

示例

如果在 URL 的使用者名稱部分出現任何無效的 URL 字元,則這些字元將被百分比編碼。

在以下示例中,我們分配了一個在使用者名稱部分包含無效字元的 URL。

const http = require('url');

const myURL = new URL('https://Ni`khi`l:hyd@tutorialspoint.com');
console.log("The URL: " + myURL.href);
console.log("Username portion of the URL is: " + myURL.username);

輸出

正如我們在輸出中看到的,URL 中的無效字元已進行百分比編碼。

The URL: https://Ni%60khi%60l:hyd@tutorialspoint.com/
Username portion of the URL is: Ni%60khi%60l
nodejs_url_module.htm
廣告

© . All rights reserved.