Open In App
Related Articles

Node.js URL Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
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.

Example:

Javascript

// Create a test URLSearchParams object
const searchpar = new URLSearchParams("keya=vala&keyb=valb");
 
// Display the key/value pairs
for (let pair of searchpar.entries()) {
    console.log(pair[0] + ', ' + pair[1]);
}

                    

Output:

keya, vala
keyb, valb

The Complete list of URL is listed below:

Methods

Description

 URL()It provides utilities for URL resolution and parsing
url.toString()It is an inbuilt application programming interface(API) of the URL module
URL.toJSON()Return the serialized URL of the URL object. 
url.domainToUnicode()Get the domain for the particular ASCII value.

API

Description

hash Get and set the fragment portion of the URL.
host APIGet and set the host portion of the URL.
origin APIThis url.origin is an inbuilt application programming interface(API) of the URL class
username APIGet and set the username of URL. 
search APIGet and set the query part of URL. 
port APIGet and set the port portion of the URL.the port value may be a number
pathname APIGet and set the pathname portion of the URL.
password APIGet and set the password portion of the URL. 
href APIIt is an Inbuilt application programming interface of class URL within the url module
hostname APIGet and set the hostname portion of the URL.
pathToFileURL APIConverts the path to a file and ensures that the URL control characters (/, \, : ) 
fileURLToPath APIDecodes the file URL to a path string and ensures that the URL control characters (/, %)
parse(urlString, parseQueryString, slashesDenoteHost) It takes a URL string, parses it, and it will return a URL object with each part of the address as properties.
Special Schemes of protocol APIGet and set the protocol scheme of the URL.
protocolGet and set the protocol portion of the URL.
domainToASCIIIt is an inbuilt application programming interface of class URL within url module.
domainToUnicodeIt is an inbuilt application programming interface of class URL within url module.
resolve(from,to)It is an inbuilt method of class URL that resolves a target URL relative to a base URL.
format(urlObject)It is an inbuilt API provided by URL class, which takes an object or string 
formatWith the help of url.format()method, we are able to format the hostname according to our needs.

Class: URLSearchParams

Class: URLSearchParams

Description

append()This method adds a key/value pair specified by the user.
delete()This method deletes the parameter specified by the user and all its associated values.
entries()Returns an iterator that allows iterating through all the key/value pairs present in the object.
forEach()Returns an iterator that allows iterating through all values contained in this object with the help of a callback function.
get()Returns the first value of the input search parameter.
get()Get the value for a particular name entry present in the URL search params object.
getAll()Returns all the values of the input search parameter in the form of an array.
sort()This method uses a stable sorting algorithm.
toString()Get the object of uri search params to the object as a string.
keys()Get the iterator object containing all the name entries only of the URL search params object.
values()In the URLSearchParams interface, the values() method returns an iterator 
set()Sets the value given as an input parameter.
toString()Returns a query string that is suitable for use in a URL.
keys()In the URLSearchParams interface, the keys() method returns an Iterator 
has()In the URLSearchParams interface, the has() method returns a Boolean 

Legacy urlObject:

authReturn the string of parameters.
hashIt is used for identifying the “fragment” portion of a URL.
hostReturn the hostname in the URL as a string.
searchGet the search query within the hostname which is followed by the ‘?’ character.
protocolIt is used by the given hostname.
hrefReturn the complete URL string along with the protocols(HTTP) and pathname or other search terms.
queryCheck whether the query is a string or an object.
slashesIt is used when one wants to check whether there is a need for ASCII valued forward-slash character (/) after the colon (:) 
portGet the numeric port portion of the host component within the hostname.
pathname It is used by the given hostname.


Last Updated : 04 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads