Node | urlObject.port API

The urlObject.port() method in Node is used to get the numeric port portion of the host component within the hostname.
The URL’s port number is returned otherwise None if the port number is not present in the URL.


Syntax : urlObject.port()
Return : Returns the URL’s port number or None

Example #1 : In these examples, we have shown how the urlObject.port() method is able to extract the url’s port number from the hostname.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Importing the module 'url' 
const url = require('url'); 
      
var adr = 
  
// Parse the address: 
var q = url.parse(adr, true); 
  
/* The parse method returns an object containing 
URL properties */
  
console.log(q.port); 

chevron_right


Output :

8080

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Importing the module 'url' 
const url = require('url'); 
      
var adr = 
  
// Parse the address: 
var q = url.parse(adr, true); 
  
/* The parse method returns an object containing 
URL properties */
  
console.log(q.port); 

chevron_right


Output :

null


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.