當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js URL.password用法及代碼示例

url.password是類的內置應用程序編程接口URL之內網址模塊用於獲取和設置 URL 的密碼部分。

用法:

const url.password

返回值:它獲取並設置 URL 的密碼部分。
以下示例程序旨在說明使用url.password方法:
示例1:

javascript


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

輸出:

示例 2:

javascript


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

輸出:

注意上述程序將使用以下命令編譯並運行myapp.js命令在節點中。
參考:
https://nodejs.org/api/url.html#url_url_password



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js URL.password API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。