• Node.js Video Tutorials

NodeJS - urlObject.path Property



The NodeJS urlObject.path property of urlObject is a combination of both pathname and search portions. This property specifies the path segment along with the search portion if present. This property does not perform the decoding of the path segment.

For instance, consider a URL “https://user:pass@site.com:80000/例?q=pas#hash”.

  • “/例?q=pas” is the path segment.

  • “/例” is the pathname portion.

  • “?q=pas” is the search portion.

The path segment contains a character ‘例’, if it is decoded the value will be “xn--fsq/”. As the NodeJs path property does not perform the decoding, the returned value will be “https://user:pass@Site.com/例#hash”.

Syntax

Following is the syntax of the NodeJS urlObject.path property

urlObject.path

Parameters

This property does not accept any parameters.

Return Value

This property retrieves the path segment of a URL along with the search portion if present.

Example

If the specified URL contains a path segment, the NodeJS path property will retrieve that segment.

In the example below, we are trying to get the path segment from the specified URL.

const url = require('url');
let address = 'https://user:pass@site.com:80000/pa/th#hashh';
let result = url.parse(address, true);
console.log(result.path);

Output

As we can see in the output below, the NodeJS path property retrieved the path segment from the URL.

/pa/th

Example

If the provided URL contains the path segment, the path property will retrieve that segment including the search portion if present.

In the following example, we are also including the search portion along with the path segment to the URL.

const url = require('url');
let address = 'https://user:pass@site.com:80000/pa/th?q=val#hashh';
let result = url.parse(address, true);
console.log(result.path);

Output

Following is the output of the above code

The path property will retrieve the path segment along with search portions as it is present in the given URL.

/pa/th?q=val

Example

If the provided URL string is not parsed using the NodeJS parse() method, the NodeJS path property will be undefined.

In the following example, we are not parsing the URL string.

const url = require('url');
let address = 'https://user:pass@site.com:8000/pa/th?q=val#hashh';
console.log(address.path);

Output

Following is the output of the above code

undefined
nodejs_url_module.htm
Advertisements