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:
- urlString: It holds the URL string which needs 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 threw TypeError.
- If auth property exists but not decoded then it threw URIError.
Example 1:
javascript
const url = require( 'url' );
let urlObject = url.parse(address, true );
console.log( 'URL Object returned after parsing' );
console.log(urlObject)
|
Output:
Example 2: This example illustrates the properties of url object.
javascript
const url = require( 'url' );
const address =
let urlObject = url.parse(address, true );
console.log( 'Url host' );
console.log(urlObject.host);
console.log( 'Url pathname' );
console.log(urlObject.pathname);
console.log( 'Url search' );
console.log(urlObject.search);
let queryData = urlObject.query;
console.log(queryData);
console.log( 'Url query object' );
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
Last Updated :
14 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...