• Node.js Video Tutorials

NodeJS - url.password Property



This NodeJS url.password property of URL class allows you to get and set the password portion of the given URL. This property will throw a TypeError if the input URL is not valid URL.

Syntax

Following is the syntax of the NodeJS password property of URL class

URL.password

Parameters

This property does not accept any parameters.

Return Value

This property allows to set and get the password segment of the given URL.

Example

If we assign a URL to the NodeJS url.password property, it will return only the password portion from the given URL.

In the program below, we are trying to get the password segment from the input URL.

const url = require('url');

const myURL = new URL("https://xyz:pass@dot.com");
console.log("Password segment in the URL: " + myURL.password);

Output

After executing the above program, the password property gets the password segment from the provided URL.

Password segment in the URL: pass

Example

The NodeJS password property will allow to set the password to the password segment in the given URL.

In the below program, we are trying to set the password to the password portion in the URL.

const url = require('url');

const myURL = new URL("https://xyz:pass@dot.com");
console.log("Password segment in the URL: " + myURL.password);

myURL.password = "T$123";
console.log("After setting the password to the password segment in the URL");
console.log(myURL.href);

Output

As we can see in the output below, the input password had been assigned to the password segment in the URL.

Password segment in the URL: pass
After setting the password to the password segment in the URL
https://xyz:T$123@dot.com/

Example

If we assign a value that is not a valid URL type to the password property, then the password property will throw a TypeError.

In the given program, we are assigning an instance of an object instead of a URL to the password property.

const url = require('url');

const myURL = new URL({});
console.log("Password segment in the URL: " + myURL.password);

TypeError

As we can see in the output below, the password property throws a TypeError as the assigned URL is not valid.

node:internal/url:564
  throw new ERR_INVALID_URL(input);
  ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:387:5)
    at URL.onParseError (node:internal/url:564:9)
    at new URL (node:internal/url:640:5)
    at Object.<anonymous> (C:\Users\Lenovo\Desktop\JavaScript\nodefile.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  input: '[object Object]',
  code: 'ERR_INVALID_URL'
}
nodejs_url_module.htm
Advertisements