Open In App

Node.js url.parse(urlString, parseQueryString, slashesDenoteHost) API

The url.parse() method takes a URL string, parses it, and it will return a URL object with each part of the address as properties.
 

Syntax: 



url.parse( urlString, parseQueryString, slashesDenoteHost)

Parameters: This method accepts three parameters as mentioned above and described below: 

Return Value: The url.parse() method returns an object with each part of the address as properties.
Note: 
 



 

Example 1: 
 




// Node program to demonstrate the  
// url.parse() method  
      
// Importing the module 'url'
const url = require('url');
  
// URL address
  
// Call parse() method using url module
let urlObject = url.parse(address, true);
  
console.log('URL Object returned after parsing');
  
// Returns an URL Object
console.log(urlObject)

Output: 
 

Example 2: This example illustrates the properties of url object. 
 




// Node program to demonstrate the  
// url object properties  
  
// Get different parts of the URL
// using object properties
const url = require('url');
  
// URL address
const address = 
  
// Call parse() method using url module
let urlObject = url.parse(address, true);
  
console.log('Url host');
  
// Returns 'geeksforgeeks.org'
console.log(urlObject.host); 
console.log('Url pathname');
  
// Returns '/projects'
console.log(urlObject.pathname); 
console.log('Url search');
  
// Returns '?sort=newest&lang=nodejs'
console.log(urlObject.search); 
   
// Get query data as an object
// Returns an object: 
// { sort: 'newest', lang: 'nodejs' }
let queryData = urlObject.query; 
console.log(queryData);
console.log('Url query object');
  
// Returns 'nodejs'
console.log(queryData.lang); 

Output: 
 

Note: The above program will compile and run by using the node myapp.js command.
Reference: https://nodejs.org/docs/latest/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
 


Article Tags :