DEV Community

Irtiza Ali
Irtiza Ali

Posted on

Validate a URL/Domain using Node.js HTTPS Module

Overview

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

Scenarios

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

  1. You want to upload data to a server with a URL like this (xyz.com) and you are not sure whether this server is secure or not.

  2. You have developed a B2B service and you want to only serve requests from valid domains.

How to do it?

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

  • https module
  • ssl-validate module

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: 'github.com/',
  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.end();

req.on('error', function(e) {
  console.error(e);
});
Enter fullscreen mode Exit fullscreen mode

Keypoints

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).
Enter fullscreen mode Exit fullscreen mode

2. ssl-validate Module

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

Top comments (3)

Collapse
 
kolysg profile image
Cosmos_K

This is a copy from a medium article.

Collapse
 
aliartiza75 profile image
Irtiza Ali

Yes that is also written by me

Collapse
 
umairakram206 profile image
Info Comment hidden by post author - thread only accessible via permalink
Umair Akram

Good work!!!
We are providing Best Digital Marketing Services in Lahore

Some comments have been hidden by the post's author - find out more