Node | urlObject.host API

The utilities for URL resolution and parsing is provided by the URL module.
A URL string is a structured string that contains various multiple meaningful components. When parsed, a URL object is returned that contains properties for each of these components.

url.host() return the host name in the url as a string.
Examples:



http://localhost:8080/register
localhost:8080 - is the host name.
https://geeksforgeeks.org/practice
geeksforgeeks.org - is the host name.

In the below example we first create a URL object. Then after using the .host() function, we will get the hostname in the URL as output.

filter_none

edit
close

play_arrow

link
brightness_4
code

//Importing the url module
const url=require('url');
  
//creating a new url object
var link = new URL("https://google.com/coding_challenges");
  
//Using the .host() function to print the host name in the url
console.log(link.host);

chevron_right


OUTPUT:
google.com


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


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