• Node.js Video Tutorials

NodeJS - url.urlToHttpOptions() Method



The NodeJS url.urlToHttpOptions() method of URL class accepts a URL object and converts this URL object into an ordinary options object that contains data every segment from the URL.

To get a better understanding of segments in a URL. Assume a serialized URL “https://user:password@site.com:80/pa/th?a=bc#footer”.

  • “https:” is the protocol segment.

  • “user:password” is the auth segment.

  • “site.com” is the hostname segment.

  • “80” is the port segment.

  • “/pa/th” is the pathname segment.

  • “/pa/th?a=bc” is the path segment.

  • “a=bc” is the search segment.

  • “#footer” is the hash segment.

Syntax

Following is the syntax of the NodeJS url.urlToHttpOptions() method of URL class

url.urlToHttpOptions(url)

Parameters

  • url: This parameter holds a WHATWG URL object that will be converted to an options object.

Return Value

This method converts a WHATWG URL object to an options object.

Following are the properties present in the returned options object

  • protocol: Specifies the protocol segment of the URL.

  • hostname: Specifies hostname segment of the URL that may contain a domain name or IP address of a server.

  • hash Specifies the fragment segment of the URL.

  • search: Specifies the serialized query segment of the URL.

  • pathname: Specifies the pathname segment of the URL.

  • path: Specifies the path segment of the URL. If the path segment in input URL contains illegal characters, an exception will be thrown.

  • href: Specifies the serialized URL.

  • port: Specifies the port segment of the URL.

  • auth: Specifies the username and password segment of the URL.

Example

If we pass a URL object to NodeJS url.urlToHttpOptions() method, it will convert the input URL object into an object which specifies every segment in the URL.

In the following example, we are trying to convert an URL object into an options object by using NodeJS url.urlToHttpOptions() method.

const { urlToHttpOptions } = require('node:url');

const myURL = new URL("https://user:password@こんにちは:80/pa/th?a=bc#footer");

console.log(urlToHttpOptions(myURL));

Output

Following is the output of the above program

internal/modules/cjs/loader.js:596
    throw err;
    ^

Error: Cannot find module 'node:url'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
    at Function.Module._load (internal/modules/cjs/loader.js:520:25)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/cg/root/63a2e36702387/main.js:1:92)
    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)
    

Note: To get the accurate result, better execute the above code in local.

As we can see in the output, the NodeJS urlToHttpOptions() method converted the input URL object into an ordinary options object.

{
  protocol: 'https:',
  hostname: 'xn--28j2a3ar1p',
  hash: '#footer',
  search: '?a=bc',
  pathname: '/pa/th',
  path: '/pa/th?a=bc',
  href: 'https://user:password@xn--28j2a3ar1p:80/pa/th?a=bc#footer',
  port: 80,
  auth: 'user:password'
}
nodejs_url_module.htm
Advertisements