Node.js URL() Method

18 Aug, 2020
The ‘url’ module provides utilities for URL resolution and parsing. The getters and setters implement the properties of URL objects on the class prototype, and the URL class is available on the global object.
The new URL() (Added in v7.0.0, v6.13.0) method is an inbuilt application programming interface of the URL module which creates a new URL object by parsing the input relative to the base. If the base is passed as a string, it will be parsed equivalent to new URL(base).
Syntax:
new URL(input[, base])
The ‘url’ module can be accessed using:
const url = require('url');
Parameters: This method accepts two parameters as mentioned above and described below:
input <string>: It is the input which is string type that is used to parse the absolute or relative input URL. The base is required if the input is relative and ignored if the input is absolute.
base <string> | <URL>: It is the base URL which is either of string type or URL, used to resolve against if the input is absolute or not.
Return Value: It returns the new URL generated along with an array of data like hostname, protocol, pathname, etc.  
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the // new URL() method   // Using require to access url module const url = require('url');  const newUrl = new URL(    'https://geeksforgeeks.org/p/a/t/h?query=string#hash');  // url array in JSON Formatconsole.log(newUrl);  const myUR = url.parse(    'https://geeksforgeeks.org/:3000/p/a/t/h?query=string#hash');console.log(myUR);console.log(URL === require('url').URL);  const myURL1 = new URL(    { toString: () => 'https://geeksforgeeks.org/' });  console.log(myURL1.href)
Output:
URL {href: ‘https://geeksforgeeks.org/p/a/t/h?query=string#hash’,origin: ‘https://geeksforgeeks.org’,protocol: ‘https:’,username: ”,password: ”,host: ‘geeksforgeeks.org’,hostname: ‘geeksforgeeks.org’,port: ”,pathname: ‘/p/a/t/h’,search: ‘?query=string’,searchParams: URLSearchParams { ‘query’ => ‘string’ },hash: ‘#hash’}Url {protocol: ‘https:’,slashes: true,auth: null,host: ‘geeksforgeeks.org’,port: null,hostname: ‘geeksforgeeks.org’,hash: ‘#hash’,search: ‘?query=string’,query: ‘query=string’,pathname: ‘/:3000/p/a/t/h’,path: ‘/:3000/p/a/t/h?query=string’,href: ‘https://geeksforgeeks.org/:3000/p/a/t/h?query=string#hash’}truehttps://geeksforgeeks.org/
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the // new URL() method   // Using require to access url module const url = require('url');const parseURL = url.parse('https://geeksforgeeks.org:3000/p/a/t/h?query=string#hash');  console.log("1 =>", parseURL)  // Prints parsed URLconst newUrl1 = new URL('https://gfg.com');  // prints https://xn--g6w251d/console.log("2 =>", newUrl1.href)  const myURL = new URL('/alfa',    'https://akash.org/');console.log("3 =>", myURL.href)  // https://akash.org/alfalet newUrl3 = new URL('http://Gfg.com/',    'https://gfg.org/');  // Prints http://gfg.com/console.log("4 =>", newUrl3.href)  newUrl4 = new URL('https://Gfg.com/',    'https://gfg.org/');  // Prints https://gfg.com/console.log("5 =>", newUrl4.href)  newUrl5 = new URL('foo://Geekyworld.com/',    'https://geekyworld.org/');// prints foo://Geekyworld.com/console.log("6 =>", newUrl5.href)  newUrl6 = new URL('http:Akash.com/',    'https://akash.org/');// prints http://akash.com/console.log("7 =>", newUrl6.href)  newUrl10 = new URL('http:Chota.com/',    'https://bong.org/');// prints http://bong.com/console.log("8 =>", newUrl10.href)  newUrl7 = new URL('https:Chota.com/',    'https://bong.org/');// prints https://bong.org/Chota.com/console.log("9 =>", newUrl7.href)  newUrl8 = new URL('foo:ALfa.com/',    'https://alfa.org/');  // Prints foo:ALfa.com/console.log("10 =>", newUrl8.href)
Run index.js file using the following command:
node index.js
Output:
1 => Url {protocol: ‘https:’,slashes: true,auth: null,host: ‘geeksforgeeks.org:3000’,port: ‘3000’,hostname: ‘geeksforgeeks.org’,hash: ‘#hash’,search: ‘?query=string’,query: ‘query=string’,pathname: ‘/p/a/t/h’,path: ‘/p/a/t/h?query=string’,href: ‘https://geeksforgeeks.org:3000/p/a/t/h?query=string#hash’}2 => https://gfg.com/3 => https://akash.org/alfa4 => http://gfg.com/5 => https://gfg.com/6 => foo://Geekyworld.com/7 => http://akash.com/8 => http://chota.com/9 => https://bong.org/Chota.com/10 => foo:ALfa.com/
Reference: https://nodejs.org/api/url.html#url_new_url_input_base
Node-URL
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.readFileSync() Method
Node.js Date.format() API
Difference between spawn() and fork() methods in Node.js
Convert xml data into json using Node.js
How to download a file using Node.js?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React