Open In App

Node.js URL() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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(
  
// url array in JSON Format
console.log(newUrl);
  
const myUR = url.parse(
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’
}
true
https://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(
  
console.log("1 =>", parseURL)
  
// Prints parsed URL
const newUrl1 = new URL('https://gfg.com');
  
console.log("2 =>", newUrl1.href)
  
const myURL = new URL('/alfa',
    'https://akash.org/');
console.log("3 =>", myURL.href)
  
let 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/',
console.log("6 =>", newUrl5.href)
  
newUrl6 = new URL('http:Akash.com/',
    'https://akash.org/');
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/');
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/alfa
4 => 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



Last Updated : 18 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads