Open In App

Node.js URL() Method

Last Updated : 18 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
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



Similar Reads

Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
What is URL Locator & what are common schemes used for the URL in HTML ?
Every HTML document on the internet has a unique URL. URL stands for Uniform Resource Locator. The URL is the HTML document's address on the internet. Any file that can be accessed via the internet has a URL, including images, JavaScript files, CSS files, Flash files, and so on. To view an HTML document in a browser, enter the URL of the document i
3 min read
Node.js URL.toJSON() Method
The url.toJSON() method in the node.js URL module is used to return the serialized URL of the URL object. The return value of this method is equivalent to the URL.href and url.toString() methods. If an URL object is serialized using JSON.stringify() method then it is called automatically. Syntax: url.toJSON() Parameters: This method does not accept
2 min read
Node url.toString() Method
The url.toString() method is an inbuilt application programming interface(API) of the URL module within the Node.JS. The url.toString() method is used to return the serialized URL. The returned value is equivalent to that of url.href and url.toJSON(). Syntax: url.toString() Parameters: This method accepts single parameter as mentioned above and des
1 min read
Node.js url.domainToUnicode() Method
The url.domainToUnicode() method is an inbuilt application programming interface of class URL with in url module which is used to get the domain for the particular ASCII value. Syntax: const url.domainToUnicode(domain) Parameter:s This method takes ASCII value as a parameter which is going to be converted in Unicode. Return value: This method retur
2 min read
Node.js Http2ServerRequest.url Method
The Http2ServerRequest.url is an inbuilt application programming interface of class Http2ServerRequest within http2 module which is used to get the Request URL string. This contains only the URL that is present in the actual HTTP request. Syntax: const request.url Parameters: This method does not accept any argument as a parameter. Return Value: Th
3 min read
Node.js http.IncomingMessage.url Method
The http.IncomingMessage.url is an inbuilt application programming interface of class Incoming Message within http module which is used to get the request URL string and also used for validating the particular URL. Syntax: request.url Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the request
2 min read
Node.js URL.domainToUnicode
The url.domainToUnicode is an inbuilt application programming interface of class URL with in url module. It returns the Unicode serialization of the domain. If the domain is invalid, the empty string is returned. Syntax : const url.domainToASCII Domain value : string Return value : string Example : const url = require('url'); console.log(url.domain
1 min read
Node.js URL.format(urlObject) API
The URL.format(urlObject) is the inbuilt API provided by URL class, which takes an object or string and return a formatted string derived from that object or string. Syntax: const url.format(urlObject) If the urlObject is not an object or string, then it will throw a TypeError. Return value: It returns string derived from urlObject. The urlObject c
4 min read
Node.js URL.hash API
The url.hash is an inbuilt application programming interface of class URL within url module which is used to get and set the fragment portion of the URL. Syntax: url.hash Return value: It gets and sets the fragment portion of the URL. Below programs illustrate the use of url.hash Method: Example 1: C/C++ Code // node program to demonstrate the // u
1 min read