• Node.js Video Tutorials

NodeJS - urlObject.protocol Property



The NodeJS urlObject.protocol property of urlObject specifies the lower-cased protocol scheme of a URL.

A scheme is a major segment of a URL. The scheme specifies the protocol to be used to access the resource on the Web (Internet). The scheme can be either HTTP or HTTPS.

For instance, consider the URL “https://user:pass@site.com:8000/pa/th#hashh”.

  • “https:” is the protocol scheme.

Syntax

Following is the syntax of the NodeJS urlObject.protocol property

urlObject.protocol

Parameters

This property does not accept any parameters.

Return Value

This property returns the lower-cased protocol scheme of a URL.

Example

If the provided URL includes the protocol segment, the NodeJS urlObject.protocol property retrieves that segment.

In the following example, we are trying to get the protocol segment from the given URL.

const url = require('url');
let address = 'https://user:pass@site.com:80000/pa/th?q=val#hashh';
let result = url.parse(address, true);
console.log(result.protocol);

Output

As we can see in the output below, the NodeJS protocol property retrieved the protocol segment from the URL.

https:

Example

If the protocol is present in the specified URL, the protocol property will return null.

const url = require('url');
let address = '//user:pass@site.com:80000/pa/th?q=val#hashh';
let result = url.parse(address, true);
console.log(result.protocol);

Output

As we can see in the output below, the protocol property is null as there is no protocol segment involved in the URL.

null

Example

If the provided URL is not parsed using the parse() method, the protocol property will retrieve undefined.

In this example, we are trying to get the protocol property from the URL without parsing it.

const url = require('url');
let address = '//user:pass@site.com:80000/pa/th?q=val#hashh';
console.log(address.protocol);

Output

As we can see in the output below, the protocol property is undefined.

undefined
nodejs_url_module.htm
Advertisements