• Node.js Video Tutorials

NodeJS - URL Module



The NodeJS URL module provides us with utilities for the resolution and parsing of the URL. The URL string is a structured string that contains multiple segments. Following is a sample URL that has multiple segments.

The URL = "http://user:pass@site.com:80/pa/th?q=val#hash".

  • ‘http:’ specifies the protocol segment.

  • ‘user’ specifies the username segment.

  • ‘pass’ specifies the password segment.

  • ‘site.com:80’ specifies the host segment.

  • ‘site.com’ specifies the hostname portion.

  • ‘80’ specifies the port portion.’

  • ‘/pa/th?q=val’ specifies the pathname segment.

  • ‘/pa/th’ specifies the path portion.

  • ‘?q=val’ specifies the search portion.

  • ‘#bar’ specifies the hash segment.

Including the URL module

To include the URL module, add the below-given syntax at the beginning of your Node.js document.

Syntax

const url = require('url');

The URL module is available on the global object so that we can use it without accessing or importing it.

Now, let us see some operations using the methods in the NodeJS URL Module.

Printing the serialized URL

To print the given serialized URL, we need to use the NodeJS url.href property.

Example

console.log("The given URL -");
const myURL = new URL('https://www.tutorialspoint.com/index.htm');
console.log(myURL.href);

Output

On executing the given program, the output is displayed as follows

The given URL - https://www.tutorialspoint.com/index.htm

Printing hostname from the URL

To print the hostname segment from the URL, we use the NodeJS url.hostname property.

Example

const myURL = new URL('https://www.tutorialspoint.com:100/index.htm');
console.log("The URL: " + myURL.href);
console.log("Hostname of the URL: " + myURL.hostname);

Output

Let us compile and run the program, to produce the following result

The URL: https://www.tutorialspoint.com:100/index.htm
Hostname of the URL: www.tutorialspoint.com

Printing the pathname from the URL

To print the pathname segment from the URL, we use the NodeJS url.pathname property.

Example

const myURL = new URL("https://www.tutorialspoint.com/prime-pack/cloud-computing/index.asp");
console.log("The URL: " + myURL.href);
console.log("Pathname of the URL: " + myURL.pathname);

Output

On executing the given program, the output is displayed as follows

The URL: https://www.tutorialspoint.com/prime-pack/cloud-computing/index.asp
Pathname of the URL: /prime-pack/cloud-computing/index.asp

The URL Class

The NodeJS URLSearchParams class provides us with utility methods. Using those methods we can read and write the query segment of the URL.

List of Properties

Following is a list of properties available in the URL Class

Sr.No Method & Description
1

hash

Used to get and set the fragment portion of the URL.

2

host

Used to get and set the host portion of the URL.

3

hostname

Used to get and set the hostname portion of the specified URL .

4

href

Used to get and set the serialized URL.

5

origin

Used to get the read-only serialization of the URL’s origin.

6

password

Used to get and set the password portion of the given URL.

7

pathname

Used to get and set the pathname portion of the given URL.

8

port

Used to set and get the port portion of the provided URL.

9

protocol

Used to get and set the protocol portion of the specified URL.

10

search

Used to get and set the serialized query portion of the URL.

11

username

Used to get and set the username of the provided URL.

List of Methods

Following is a list of methods available in the URL Class

Sr.No Method & Description
1

toString()

Used to retrieve the serialized URL from the URL object.

2

toJSON()

Used to retrieve the serialized URL from the URL object.

3

domainToASCII()

Used to return the Punycode which converts and represents the domain as ASCII characters.

4

domainToUnicode

Used to return the Unicode for the ASCII values present in the domain.

5

fileURLToPath

Used to accept a file URL string or URL object and converts them as a properly encoded path.

6

format()

Used to return a customizable serialization of a URL string that is a representation of a WHATWG URL object

7

pathToFileURL()

Used to accept a path of a file and converts them into a fully-resolved file URL Object

8

urlToHTTPOptions()

Used to convert a URL object into an ordinary options object that contains data every segment from the URL.

9

format(urlObject)

Used to return a formatted URL string that is derived from the urlObject.

10

parse()

Used to accept a URL string, parses it, and finally, it returns an object containing the segments present in the provided URL string.

The URLSearchParams Class

The NodeJS URL class provides us with methods and properties for URL parsing and resolution. Using those, we can get and set the segments present in the URL. This class is available on the global object.

List of Methods

Following is a list of methods available in the URLSearchParams Class

Sr.No Method & Description
1

new URLSearchParams (string)

Used to create a query string object with a JSON object.

2

new URLSearchParams(obj)

Used to parse the input string as a query string and uses it to create a new query string object.

3

append()

Used to append a specified name/value pair as a new search parameter in the query string.

4

delete()

Used to remove all name-value pairs whose value is name.

5

entries()

Used to returns an ES6 Iterator that allows iteration through all the name-value pairs present in the query string.

6

forEach()

Used to iterate through all the name-value pairs in the query and invokes the given function.

7

get()

Used to retrieve the value from a specified name in the query string.

8

getAll()

Used to get all the values of a specified name in the query string.

9

has()

It will prints true if the name passed to this method is present, else it prints false.

10

keys()

Used to return an ES6 Iterator over the names of each name-value pair.

11

set()

Used to set the name-value pairs to the query string.

12

sort()

Used to sort all the names of the name-value pairs in the query string.

13

toString()

Used to retrieve a serialized string containing the search parameters of a query segment.

14

values()

Used to return an ES6 iterator that allows iteration through all the values of each name-value pair

15

[Symbol.iterator]()

Used to return an ES6 Iterator that allows iteration through all the name-value pairs present in the query string.

List of Properties

Following is the list of properties available in the urlObject

Sr.No Method & Description
1

auth

Used to specify the auth segment of the URL

2

hash

Used to specify the fragment segment from the URL.

3

host

Used to specify the complete lower-cased host segment of a URL.

4

hostname

Used to specify the lower-cased host name portion of the host segment of a URL.

5

href

Used to specify the full lower-case URL string that is parsed with both protocol and host segments.

6

path

Used to specify the path segment along with the search portion if present.

7

pathname

Used to specify the pathname portion from the path segment of the URL

8

port

Used to specify the numeric port portion of the host segment from a URL.

9

protocol

Used to specify the lower-cased protocol scheme of a URL.

10

query

Used to specify the query string portion from the URL.

11

search

Used to specify the complete query string segment of a URL.

12

slashes

It returns a Boolean value based on the two ASCII forward slashes (//) that are required after the colon (:) in the protocol segment of the URL.

Advertisements