NodeJs HTTPS.Get Example with Error Handling

Posted on September 6, 2019 at 9:51 am

Here is an example of using NodeJs “https” library with error handling:

var dateformat = require('dateformat');
var https = require('https');
 
try 
{
    var httpsget = https.get('https://www.google.com/', function(res) 
    {
        console.log('[HTTPS.Get] %s StatusCode:', dateformat(new Date(), "yyyy-mm-dd h:MM:ss"), res.statusCode);
    }).on("error", function(error) 
    {
        console.log('[HTTPS.Get] %s Error:', dateformat(new Date(), "yyyy-mm-dd h:MM:ss"), error.message);
    });
} 
catch(e) 
{
    console.log('[HTTPS.Get] %s Error:', dateformat(new Date(), "yyyy-mm-dd h:MM:ss"), e.message);
}

It should output something like this:

[HTTPS.Get] 2019-09-06 9:45:07 StatusCode: 200

Receive updates via email

Other Posts

Updated Posts