• Node.js Video Tutorials

NodeJS - url.hostname Property



The NodeJS url.hostname property of URL class of the URL module allows us to get and set the hostname portion of the specified URL.

The NodeJS url.hostname property looks similar to the NodeJS host property but the key difference between hostname and host is that the hostname doesn’t include the port segment. If any invalid hostname values are assigned to the hostname property will be ignored.

Syntax

Following is the syntax of the NodeJS hostname property of URL class

URL.hostname

Parameters

This property does not accept any parameters.

Return Value

This property allows to gets and sets the hostname portion of the specified URL.

The Below examples demonstrate the usage of the Node.js URL.hostname property of the path module.

Example

Using the NodeJS URL.hostname property, we can get the hostname portion of the provided URL.

In the following example, we are trying to get the hostname portion of the given URL.

const url = require('url');

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

As we can see in the output below, the URL.hostname property prints the hostname portion of the given URL.

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

Example

We cannot change the value of the hostname and port segments of the hostname portion in the URL by using the URL.hostname property.

In the example below, we are trying to modify the hostname and port segments of the hostname portion in the given URL by using the URL.hostname property.

const url = require('url');

const myURL = new URL('https://www.tutorialspoint.com:100/index.htm');
console.log("Before changing the port: " + myURL.href);

myURL.hostname = "www.tttttttttutorialspoint.com:101"
console.log("After changing hostname and the port: " + myURL.href);

Output

As we can see in the output below, the URL.hostname property haven’t made any change to the hostname portion of the given URL.

Before changing the port: https://www.tutorialspoint.com:100/index.htm
After changing hostname and the port: https://www.tutorialspoint.com:100/index.htm

Example

We can set the hostname and the port segments of the hostname portion by using the URL.host property.

In the following example, we are trying to change the hostname and port segments of the hostname portion in the given URL by using the URL.host property.

const url = require('url');

const myURL = new URL('https://www.tutorialspoint.com:100/index.htm');
console.log("Before changing the hostname and port: " + myURL.href);

myURL.host = "www.tutorialsssspoint.com:101"
console.log("After changing the hostname and port: " + myURL.href);

Output

If we compile and run the above program, the URL.host property made the specified modifications to the given URL.

Before changing the hostname and port: https://www.tutorialspoint.com:100/index.htm
After changing the hostname and port: https://www.tutorialsssspoint.com:101/index.htm
nodejs_url_module.htm
Advertisements