Node | urlObject.hostname API

The url.hostname is an inbuilt application programming interface of class URL within the url module which is used to get and set the hostname portion of the URL. The url.hostname does not include the port number.

Syntax:

url.hostname

It associates by single value url which is an object created by URL constructor.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node program to demonstrate the  
// url.hostname API as both Getter and Setter  
      
// Imports only single export 'URl'
// from url module using ES6 syntax
const { URL } = require('url');  
      
// Creating and initializing myURL
// object using URL constructor
const myURL = new URL('https://gfg.org:80/foo#ram'); 
      
// Display hostname value of myURL before change 
console.log("Before Change"); 
console.log(myURL.hostname); 
   
console.log('');
   
// Updating hostname portion 
// using hostname method
myURL.hostname = 'example2.com'
      
// Display hostname value of myURL after updating 
console.log("After Change"); 
console.log(myURL.hostname); 

chevron_right


Output:

Before Change
gfg.org

After Change
geeksforgeeks.org

Reference: https://nodejs.org/dist/latest-v10.x/docs/api/url.html#url_url_hostname



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManojVarmaPenmatsa



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.