May 16, 2020

1 min read

Validate a URL/Domain using Node.js HTTPS Module

Validate a URL/Domain using Node.js HTTPs Module.


This post provides guidelines on how to validate a domain. By validation, I mean that the domain has a valid certificate signed by Certificate Authority.


The list given below contains scenarios in which you want to validate the domain/URL:

How to do it?

In node.js there are two ways to do it:

1. HTTPS Module

Nodejs https module’s request method validates the domain provided against the chain of Certificate Authorities root certificate. A code example is given below:

var https = require('https');

var options = {
hostname: '',
port: 443,
path: '/',
method: 'GET',
rejectUnauthorized: true

var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);


req.on('error', function(e) {


rejectUnauthorized: This means that it will validate the server/domain certificate against the chain of CA's root certificate.The only problem with this approach is that this chain should be updated regularly otherwise a new domain that is signed by a certificate authority root certificate which is not part of the chain, marked as an invalid certificate(a common example is a self-signed certificate).

2. ssl-validate Module

It can also be used but it requires another module to get the domain information.