Node.js urlObject.path Property
In this article, we will be learning about urlObject.path API. urlObject.path API property is a concatenation of the pathname and search components. Pathname refers to the file path in an URL and search components refer to the query and hash string which has fixed limits such as question mark (?) or hash (#) characters decoding of the path is performed.
Example: Let us consider an URL ‘http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash’
Syntax:
urlObject.path
Return Value:
'/p/a/t/h?query=string'
urlObject.path returns ‘/p/a/t/h?query=string’ after it is returned by the url.parse() function.
Example 1: Index.js
Javascript
//Importing url module const http = require( 'url' ); // Creating a demo URL const myURL = 'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash' ; // Parsing the Address const q = http.parse(myURL, true ); // Displaying the path console.log(q.path); |
Execution command:
node index.js
Console Output:
/p/a/t/h?query=string
Example 2:(Changing the path)
Javascript
//Importing the url module const http = require( 'url' ); // Creating a demo URL const myURL = 'http://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash' ; const q = http.parse(myURL, true ); // Display path value of myURL before change console.log( "Before Change" ); console.log(q.path); console.log(); // Changing the path q.path = '/s/k/t/j?query=abc@gmail.com' // Printing the changed path console.log( "After Change" ); console.log(q.path); |
Execution command:
node index.js
Console Output:
Before Change /p/a/t/h?query=string After Change /s/k/t/j?query=abc@gmail.com
Contact Us