• Node.js Video Tutorials

NodeJS - url.pathname property



The NodeJS url.pathname property of URL class allows us to get and set the pathname segment of the given URL.

Invalid URL characters included in the pathname segment assigned to this property are percent-encoded. If an invalid URL is specified, this property will throw a TypeError.

Syntax

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

URL.pathname

Parameters

This property does not accept any parameters.

Return Value

This property can get and set the pathname portion of the provided URL.

Example

If we assign a complete URL to the NodeJS url.pathname property, it will get the pathname portion of the given URL.

In the following example, we are trying to assign a URL to the NodeJS url.pathname property.

const url = require('url');

const myURL = new URL("https://www.tutorialspoint.com/prime-pack/cloud-computing/index.asp");
console.log("Path segment of the URL: " + myURL.pathname);

Output

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

/prime-pack/cloud-computing/index.asp

Example

If we try to set the path to an URL, the pathname property will return the complete URL with provided path.

In this program below, we are modifying the path of the URL by using the pathname property.

const url = require('url');

const myURL = new URL("https://www.tutorialspoint.com/prime-pack/cloud-computing");
console.log(myURL.href)
console.log("Path segment of the URL: " + myURL.pathname);

myURL.pathname = "/Normal-pack/Node-js";
console.log("After setting the pathname to : " + myURL.pathname);

console.log("The modified path will be: " + myURL.href);

Output

As we can in the output below, the path of the input URL got modified.

https://www.tutorialspoint.com/prime-pack/cloud-computing
Path segment of the URL: /prime-pack/cloud-computing

After setting the pathname to : /Normal-pack/Node-js
The modified path will be: https://www.tutorialspoint.com/Normal-pack/Node-js

Example

If any invalid URL characters are provided in the pathname portion, the pathname property will get the URL by percent-encoding the invalid characters.

In the following example, we are assigning an URL with invalid characters in the pathname portion.

const url = require('url');

const myURL = new URL("http://www.tutorialspoint.com/Node`Js");
console.log(myURL.href)
console.log("Path segment of the URL: " + myURL.pathname);

Output

As we can see in the output, the invalid characters in the URL got percent-encoded.

http://www.tutorialspoint.com/Node%60Js
Path segment of the URL: /Node%60Js

Example

If we assign an invalid URL to the pathname property, it returns a TypeError.

In the example below, we are assigning an invalid URL to the pathname property.

const url = require('url');

const myURL = new URL("[ww.tutorialspoint.com/Node`Js");
console.log(myURL.href)
console.log("Path segment of the URL: " + myURL.pathname);

TypeError

As we can see in the output below, a TypeError is thrown by the pathname property.

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: '[ww.tutorialspoint.com/Node`Js',
  code: 'ERR_INVALID_URL'
}
nodejs_url_module.htm
Advertisements