Open In App

Node.js urlObject.protocol API

Last Updated : 14 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
With the help of urlObject.protocol() method, we can find the name of protocol which is used by the given hostname.
Syntax : urlObject.protocol() Return : Returns the protocol used (i.e. - http, https, ftp, etc.)
Example #1 : In this example, with the help of urlObject.protocol() method we are able to extract the protocol used from the hostname. javascript 1=1
// Importing the module 'url' 
const url = require('url');
  
var adr = 
'http://localhost:8080/default.htm?year=2019&month=may';

// Parse the address:
var q = url.parse(adr, true);

/* The parse method returns an object containing
 URL properties */

console.log(q.protocol);
Output : Example #2 : javascript 1=1
// Importing the module 'url' 
const url = require('url');
  
var adr = 
'https://localhost:8080/default.htm?year=2k19&month=geekofthemonth';

// Parse the address:
var q = url.parse(adr, true);

/* The parse method returns an object containing
 URL properties */

console.log(q.protocol);
Output :

Next Article

Similar Reads