Horje
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() method is able to extract the URL’s port number from the hostname. 

javascript

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

Output :

8080

Example 2 : 

javascript

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

Output :

null


Reffered: https://www.geeksforgeeks.org


Node.js

Related
Node.js URL.resolve(from,to) API Node.js URL.resolve(from,to) API
Node.js urlObject.host API Node.js urlObject.host API
Node.js URL.format(urlObject) API Node.js URL.format(urlObject) API
Node.js URL.format API Node.js URL.format API
Sequential Functionality in Node.js Sequential Functionality in Node.js

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9