url.password是URL類的內置應用程序編程接口,帶有url模塊,用於獲取和設置URL的密碼部分。句法:
const url.password
返回值:它獲取並設置URL的密碼部分。
以下示例程序旨在說明url.password方法的用法:
範例1:
// node program to demonstrate the
// url.password API as Setter
//importing the module 'url'
const http = require('url');
// creating and initializing myURL
const myURL = new URL('https://pqr:abc@example.com');
// Display password
// value of myURL before change
console.log("Before Change");
console.log(myURL.password);
// assinging password portion
// using password API
console.log();
// Changing the myUrl.password for the above URL
myURL.password = '123';
// Display the changed password
// value of myURL after change
console.log("After Change");
console.log(myURL.href);
輸出:
Before Change abc After Change https://:123@example.com:80/foo#ram
範例2:
// node program to demonstrate the
// url.password API as Getter
//importing the module 'url'
const http = require('url');
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
myURL.password = '1234'
// getting the password portion
// using password
const password = myURL.password;
// Display hostname value
console.log("password is:"+password);
console.log(myURL.href);
輸出:
password is:1234 https://:1234@example.org/foo#ram
注意:以上程序將使用Node中的myapp.js命令進行編譯和運行。
參考:
https://nodejs.org/api/url.html#url_url_password
相關用法
- Node.js URLSearchParams.set()用法及代碼示例
- Node.js URL.protocol用法及代碼示例
- Node.js URL.hostname用法及代碼示例
- Node.js URL.href用法及代碼示例
- Node.js URL.hash用法及代碼示例
- Node.js URL.host用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node | URL.password API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。