Node | URL.port API

The url.port is an inbuilt application programming interface of class URL within url module which is used to get and set the port portion of the URL.the port value may be a number or a string containing a number in the range 0 to 65535 (inclusive). Setting the value to the default port of the URL objects given protocol will result in the port value becoming the empty string (”).
The port value can be an empty string in which case the port depends on the protocol/scheme:

Protocol port
“ftp” 21
“file”
“gopher” 70
“http” 80
“https” 443
“ws” 80
“wss” 443

Upon assigning a value to the port, the value will first be converted to a string using .toString().
If that string is invalid but it begins with a number, the leading number is assigned to the port. If the number lies outside the range denoted above, it is ignored.

Syntax:

const url.port

Return value: It gets and sets the port portion of the URL.

Below programs illustrate the use of url.port Method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// node program to demonstrate the 
// url.port API as Setter 
   
//importing the module 'url'
const http = require('url');
   
// creating and initializing myURL
const myURL = new URL('https://example.com:80/foo#ram');
   
// Display href and port
// value of myURL before change
console.log("Before Change");
console.log(myURL.href);
   
// assinging port portion
// using port API
console.log();
myURL.port = '12345';
   
// Display href and password
// value of myURL after change
console.log("After Change");
console.log(myURL.href);

chevron_right


Output:

Before Change
https://example.com:80/foo#ram

After Change
https://example.com:12345/foo#ram

Example 2: If the port number is half numeric and half alphabetic

filter_none

edit
close

play_arrow

link
brightness_4
code

// node program to demonstrate the 
// url.port API as Setter 
    
//importing the module 'url'
const http = require('url');
    
// creating and initializing myURL
const myURL = new URL('https://example.com:80/foo#ram');
    
// Display href and port
// value of myURL before change
console.log("Before Change");
console.log(myURL.href);
    
// assinging port portion
// using port API
console.log();
myURL.port = '2580abcd';
    
// Display href and password
// value of myURL after change
console.log("After Change");
console.log(myURL.href);

chevron_right


Output:

Before Change
https://example.com:80/foo#ram

After Change
https://example.com:2580/foo#ram

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// node program to demonstrate the 
// url.port API as Getter 
  
//importing the module 'url'
const http = require('url');
  
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
myURL.port = '1234'
  
// getting the port portion
// using port
const port = myURL.port;
  
// Display port value 
console.log("port is : " + port);
  
//"https" default port is 443
console.log("After Change");
myURL.port = '443';
console.log(myURL.port);

chevron_right


Output:

port is : 1234

After Change
prints Empty String
// myURL.port = 443 - Default port for https
// Therefore ignored by NodeJS - URL

NOTE: The above program will compile and run by using the myapp.js command on Node.

Reference:
https://nodejs.org/api/url.html#url_url_port



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.