Node | 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 a smentioned above and described below:

  • urlString: It holds the URL string which need to parse.
  • parseQueryString: It is a boolean value. If it set to true then the query property will be set to an object returned by the querystring module’s parse() method. If it set to false then the query property on the returned URL object will be an unparsed, undecoded string. Its default value is false.
  • slashesDenoteHost: It is a boolean value. If it set to true then the first token after the literal string // and preceding the next / will be interpreted as the host. For example: //geeksforgeeks.org/web-technology contains the result {host: ‘geeksforgeeks.org’, pathname: ‘/web-technology’} rather than {pathname: ‘//geeksforgeeks.org/web-technology’}. Its default value is false.

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

Note:

  • If urlString is not a string then it thrown TypeError.
  • If auth property exists but not decoded then it threw URIError.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)

chevron_right


Output:

URL Object returned after parsing
Url {
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'geeksforgeeks.org',
    port: null,
    hostname: 'geeksforgeeks.org',
    hash: null,
    search: '?sort=newest&lang=nodejs',
    query: [Object: null prototype] { sort: 'newest', lang: 'nodejs' },
    pathname: '/projects',
    path: '/projects?sort=newest&lang=nodejs',
    href: 'https://geeksforgeeks.org/projects?sort=newest&lang=nodejs' 
}

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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); 

chevron_right


Output:

Url host
geeksforgeeks.org
Url pathname
/projects
Url search
?sort=newest&lang=nodejs
[Object: null prototype] { sort: 'newest', lang: 'nodejs' }
Url query object
nodejs

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.