How to pick up ping time with the https node module

Asked

Viewed 218 times

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

  • 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.

2 answers

0

Try :

const https = require('https');
const hostname = 'www.site.com';

setInterval(()=>{
  let startRequest  =  new Date().getTime();
  const req = https.request({
    hostname: hostname
  }, function (res){
	const pingTime =  new Date().getTime()- startRequest;
    console.log(`${res.statusCode} | ${hostname} | ${pingTime} | ${new Date().toISOString().slice(0,19).replace('T',' ')}`);
  });
  req.on('error',(e)=>{
    console.log('Erro: '+e);
  });
  req.on('connect',(e)=>{
   startRequest  =  new Date().getTime();
  });
  req.end();
},3000);

  • Is this technique as precise as possible? Given that time when it records "the request trigger" runs it before it is actually fired ?

  • 1

    I changed my suggestion, try using the connect event present in httpClientRequest. For the documentation on ( https://nodejs.org/api/http.html#http_class_http_clientrequest ) it serves to intercept the beginning of the connection between client and server, see if it works.

0

Uses the morganjs, he eh a middleware and he logs every request.

Expressjs example:

const express = require('express');
const app = express();
const morgan = require('morgan');

app.use(morgan('dev')); // Essa eh uma forma de declarar um middleware "global" para todas as rotas

app.listen(3333);

On the line where app.use(morgan('dev')) this 'dev' is the standard log q it will show in the console, has several types, but since you should not log in production, only for development and this standard('dev') serves for 90% of cases

const express = require('express');
const app = express();
const morgan = require('morgan');


app.get("/", morgan('dev'), (req, res) => {
    res.send("<h1>Hello World!</h1>");
} // Nesse caso o morgan ira mostrar e monitorar os dados dessa rota somente, onde tem o app.get pode ser qlq metodo(app.post, app.put, app...)

app.listen(3333);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.