Open In App

Node.js URL.port API

Last Updated : 14 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:  

Javascript




// 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);
   
// assigning 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);


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 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);
    
// assigning 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);


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 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);


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
 



Previous Article
Next Article

Similar Reads

Web API URL.port Property
The Web API URL.port property is used to get USVString containing the port number of the URL. Syntax: var str = URL.port Return Value: This property returns a USVString containing the port number of the URL. Example 1: C/C++ Code <!DOCTYPE html> <html> <body> <h1>GeeksforGeeks</h1> <button onclick="get()"
1 min read
Node.js 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 port number or None Example 1: In these examples, we have shown how the urlObject.port(
1 min read
How to get protocol, domain and port from URL using JavaScript ?
The protocol, domain, and port of the current page can be found by two methods: Method 1: Using location.protocol, location.hostname, location.port methods: The location interface has various methods that can be used to return the required properties. The location.protocol property is used to return the protocol scheme of the URL along with the fin
2 min read
Node.js URL.format(urlObject) API
The URL.format(urlObject) is the inbuilt API provided by URL class, which takes an object or string and return a formatted string derived from that object or string. Syntax: const url.format(urlObject) If the urlObject is not an object or string, then it will throw a TypeError. Return value: It returns string derived from urlObject. The urlObject c
4 min read
Node.js URL.hash API
The url.hash is an inbuilt application programming interface of class URL within url module which is used to get and set the fragment portion of the URL. Syntax: url.hash Return value: It gets and sets the fragment portion of the URL. Below programs illustrate the use of url.hash Method: Example 1: C/C++ Code // node program to demonstrate the // u
1 min read
Node.js URL.host API
The url.host is an inbuilt application programming interface of class URL with in url module which is used to get and set the host portion of the URL. Syntax: const url.host Return value: It gets and sets the host portion of the URL.Below programs illustrates the use of url.host Method:Example 1: C/C++ Code // node program to demonstrate the // url
1 min read
Node.js URL.hostname API
The url.hostname is an inbuilt application programming interface of class URL with in url module which is used to get and set the hostname portion of the URL. The key difference between url.host and url.hostname is that url.hostname does not include the port. Syntax: const url.hostname Return value: It gets and sets the hostname portion of the URL.
1 min read
Node.js URL.href API
The url.href is an inbuilt application programming interface of class URL with in the url module which Gets and sets the serialized URL. Getting the value of the href property is equivalent to calling the url.toString() method.Setting the value of this property to a new value is equivalent to creating a new URL object using new URL(value). Each of
2 min read
Node.js URL.password API
The url.password is an inbuilt application programming interface of class URL with in url module which is used to get and set the password portion of the URL. Syntax: const url.password Return value: It gets and sets the password portion of the URL.Below programs illustrate the use of url.password Method:Example 1: C/C++ Code // node program to dem
2 min read
Node.js URL.origin API
url.origin is an inbuilt application programming interface(API) of the URL class within the url module. url.origin API is used to gets the read-only serialization of the URL’s origin. Syntax: url.origin url : It is an object created by URL constructor. Example 1: C/C++ Code //Importing the url module const url = require('url'); //Creating an URL_1
1 min read