Node.js URL.password API
url.password是类URL的内置应用程序编程接口,带有 in 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);
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);
输出:
示例 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);
输出:
注意:上面的程序将在 Node.js 中使用myapp.js 命令编译和运行。
参考:
https://nodejs.org/api/url.html#url_url_password