• Node.js Video Tutorials

NodeJS - url.parse() Method



The NodeJS url.parse() method accepts a URL string, parses it, and finally, it returns an object containing the segments present in the provided URL string.

The returned object contains the following properties

  • protocol: This specifies the protocol scheme of the URL (ex: https:).

  • slashes: A Boolean value that specifies whether the protocol is followed by two ASCII forward slashes (//).

  • auth: This specifies the authentication segment of the URL (ex: username: password).

  • username: This specifies the username from the authentication segment.

  • password: This specifies the password from the authentication segment.

  • host: This specifies the host name with the port number.

  • hostname: This specifies the host name excluding the port number.

  • port: Indicates the port number.

  • pathname: Indicates the URL path.

  • query: This contains a parsed query string unless parsing is set to false.

  • hash: It specifies the fragment portion of the URL including the “#”.

  • href: It specifies the complete URL.

  • origin: It specifies the origin of the URL.

Syntax

Following is the syntax of the NodeJS url.parse() method

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

Parameters

This method accepts three parameters. The same are described below

  • urlString: This parameter specifies a URL string that needs to be parsed.

  • parseQueryString: This parameter specifies a Boolean value. If true, the query segment in the URL string will be set to an object. If false, then the query property will be an unparsed string. By default, the value is false.

  • slashesDenoteHost: This parameter specifies a Boolean value. If true, the first token after the literal string "//" and preceding the next "/" will be considered the host. For example, consider (//one/two), the result would be {host: ‘one’, pathname: ‘/two’} rather than {pathname: ‘//one/two’}. By default, the value is false.

Return Value

This method takes a URL string and returns an object.

  • If the urlString to be parsed is not a string, this method throws a TypeError.

  • if the auth property is present but cannot be decoded, this method throws a URIError.

Example

If we pass the Boolean value true as a parseQueryString parameter of the NodeJS url.parse() method, it sets the query property in the form of an object.

const url = require('url');
const address = 'https://user:pass@site.com:2000/pa/th?q=val#hash';
let result = url.parse(address, true);
console.log(result)

Output

Url {
  protocol: 'https:',
  slashes: true,
  auth: 'user:pass',
  host: 'site.com:2000',
  port: '2000',
  hostname: 'site.com',
  hash: '#hash',
  search: '?q=val',
  query: { q: 'val' },
  pathname: '/pa/th',
  path: '/pa/th?q=val',
  href: 'https://user:pass@site.com:2000/pa/th?q=val#hash' 
}

Example

If we pass the Boolean value false as a parseQueryString parameter of the NodeJS parse() method, it returns the query property as an unparsed string.

const url = require('url');
const address = 'https://site.com/pa/th?q=val#hash';
let result = url.parse(address, false);
console.log(result)

Output

Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'site.com',
  port: null,
  hostname: 'site.com',
  hash: '#hash',
  search: '?q=val',
  query: 'q=val',
  pathname: '/pa/th',
  path: '/pa/th?q=val',
  href: 'https://site.com/pa/th?q=val#hash' 
}

Example

If the urlString parameter of parse() method is not a string, it returns a TypeError.

Here, we are trying to parse a URL string that is not a string.

const url = require('url');
const address = 798032;
let result = url.parse(address, true);
console.log(result)

TypeError

As we can see in the output below, a TypeError is thrown by the parse() method because the provided URL string is not a string.

url.js:150
    throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type number
    at Url.parse (url.js:150:11)
    at Object.urlParse [as parse] (url.js:144:13)
    at Object.<anonymous> (/home/cg/root/63aab5fe9c383/main.js:5:18)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
    at startup (internal/bootstrap/node.js:238:19)
urlobject Properties (Legacy)
nodejs_url_module.htm
Advertisements