0
How can I perform the ping calculation between client and server using the http/https Node module.
NOTE: I use this module specifically because the purpose of the script is to generate log availability of a website every half minute with the following output:
200 | www.site.com | 350 ms | 19-08-2019 12:10:29
Feel free to indicate some module ready for this.
const https = require('https');
const hostname = 'www.site.com';
setInterval(()=>{
const req = https.request({
hostname: hostname
}, function (res){
console.log(`${res.statusCode} | ${hostname} | ${new Date().toISOString().slice(0,19).replace('T',' ')}`);
});
req.on('error',(e)=>{
console.log('Erro: '+e);
});
req.end();
},3000);
Current output:
200 | www.site.com | 19-08-2019 12:10:29
I tried to use listeners to capture the current time at the beginning of the request and subtract with time at the end of the request, but I did not find the event that is triggered nomomento in which the request is executed, and I do not know if this is the correct way to calculate ping time.
Take a look at this here -> https://stackoverflow.com/questions/4737130/how-to-ping-from-a-node-js-app
– PauloHDSousa
I believe this is not valid for logging. Because I would have to make a request to validate whether the http server responds 200 and another to validate ping time.
– ayelsew