Open In App

Node.js URL.resolve(from,to) API

Last Updated : 14 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report


The url.resolve(from, to) is inbuilt method of class URL that resolves a target URL relative to a base URL.

Syntax:

url.resolve(from, to);
    Where,

  • from: (type:String) The base URL being resolved against.
  • to : (type:String) The “href” URL being resolved.

Return value:

It returns the resolved URL by given parameters in from URL by to URL(type:string).

Parsing of Target URL:

1. Preceded by forward slash(“/”) – It will replace whole path after domain of base URL.
2. Not preceded by forward slash(“/”) – It will replace last word after forward slash(“/”) in path of base URL.

Examples:




// node program to demonstrate the  
// url.resolve(from, to) method  
    
//importing the module 'url' 
const url = require('url'); 
  
//We can directly console.log() return value of the method
  
//Method 1:
console.log(url.resolve("http://www.google.com/", "/one"));                  
console.log(url.resolve("http://www.google.com/one/two/three", "/four"));    
  
//Method 2:
console.log(url.resolve("http://www.google.com/", "one"));                  
console.log(url.resolve("http://www.google.com/one/two/three", "four"));    


OUTPUT: 
http://www.google.com/one
http://www.google.com/four

http://www.google.com/one
http://www.google.com/one/two/four

Note:

This code can run with node command in command prompt.(Eg. node file name)

References:

https://nodejs.org/api/url.html#url_url_resolve_from_to


Previous Article
Next Article

Similar Reads

How to Resolve form-data is getting undefined in Node/Express API ?
Working with forms and handling data submissions is a basic requirement in web development. When using Node and Express for building APIs, you might get an error that the FormData object is undefined. In this article, we'll explore how to resolve form data getting undefined in a Node/Express API. The FormData object is a part of the Web API and is
3 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
There are many different ways to install node.js on a computer. The simplest method to verify whether node.js has been properly installed in your computer is simply type node-v in the command prompt or Windows PowerShell. But many times, it happens, most commonly if you're a beginner, the command prompt prints the output something like this: 'node'
2 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
Node.js URL.host API
The url.host is an inbuilt application programming interface of class URL with in url module which is used to get and set the host portion of the URL. Syntax: const url.host Return value: It gets and sets the host portion of the URL.Below programs illustrates the use of url.host Method:Example 1: C/C++ Code // node program to demonstrate the // url
1 min read
Node.js URL.hostname API
The url.hostname is an inbuilt application programming interface of class URL with in url module which is used to get and set the hostname portion of the URL. The key difference between url.host and url.hostname is that url.hostname does not include the port. Syntax: const url.hostname Return value: It gets and sets the hostname portion of the URL.
1 min read
Node.js URL.href API
The url.href is an inbuilt application programming interface of class URL with in the url module which Gets and sets the serialized URL. Getting the value of the href property is equivalent to calling the url.toString() method.Setting the value of this property to a new value is equivalent to creating a new URL object using new URL(value). Each of
2 min read
Node.js URL.password API
The url.password is an inbuilt application programming interface of class URL with in url module which is used to get and set the password portion of the URL. Syntax: const url.password Return value: It gets and sets the password portion of the URL.Below programs illustrate the use of url.password Method:Example 1: C/C++ Code // node program to dem
2 min read
Node.js URL.origin API
url.origin is an inbuilt application programming interface(API) of the URL class within the url module. url.origin API is used to gets the read-only serialization of the URL’s origin. Syntax: url.origin url : It is an object created by URL constructor. Example 1: C/C++ Code //Importing the url module const url = require('url'); //Creating an URL_1
1 min read
Node.js URL.username API
URL.username is an inbuilt application programming interface(API) of the URL class within Node.JS. URL.username API is used to get and set the username of the URL. Syntax: url.username URL: It is an object created by a URL constructor. Example 1: (Getting the username of URL) C/C++ Code //Creating an URL_1 object with URL constructor. const URL_1 =
1 min read