Node.js URL.port API - GeeksforGeeks

14 Oct, 2021
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:
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:  
Javascript
// node program to demonstrate the // url.port API as Setter    //importing the module 'url'const http = require('url');   // creating and initializing myURLconst myURL = new URL('https://example.com:80/foo#ram');   // Display href and port// value of myURL before changeconsole.log("Before Change");console.log(myURL.href);   // assigning port portion// using port APIconsole.log();myURL.port = '12345';   // Display href and password// value of myURL after changeconsole.log("After Change");console.log(myURL.href);
Output: 
Example 2: If the port number is half numeric and half alphabetic  
Javascript
// node program to demonstrate the // url.port API as Setter     //importing the module 'url'const http = require('url');    // creating and initializing myURLconst myURL = new URL('https://example.com:80/foo#ram');    // Display href and port// value of myURL before changeconsole.log("Before Change");console.log(myURL.href);    // assigning port portion// using port APIconsole.log();myURL.port = '2580abcd';    // Display href and password// value of myURL after changeconsole.log("After Change");console.log(myURL.href);
Output: 
Example 3:  
Javascript
// node program to demonstrate the // url.port API as Getter   //importing the module 'url'const http = require('url');  // creating and initializing myURLconst myURL = new URL('https://example.org/foo#ram');myURL.port = '1234'  // getting the port portion// using portconst port = myURL.port;  // Display port value console.log("port is : " + port);  //"https" default port is 443console.log("After Change");myURL.port = '443';console.log(myURL.port);
Output: 
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 
mridulmanochagfg
sagartomar9927
Node-URL
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Node.js fs.readFileSync() Method
How to update Node.js and NPM to next version ?
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript