Skip to content
Related Articles

Related Articles

Improve Article

Node urlObject.path Property

  • Difficulty Level : Easy
  • Last Updated : 05 Jan, 2021

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.No 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: '/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 = 
  
// Parsing the Address
var 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 = 
  
var 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :