0
How to calculate the response time of an application in Node.js
?
Without much delay only the request being made outside and a method that returns time through the Date.now()
Example:
Web application:
function calcular() {
var dataRequest = Date.now();
$.ajax({
url: 'https://rotaqueretornatempo/tempo,
contentType: 'application/json',
method: 'GET',
dataType: 'json',
success: function (data) {
console.log(data);
let tempo = Math.floor((data.resposta - dataRequest) / 1000)
$("#demo").text(`Método: ${dataRequest} | Server: ${data.resposta}`)
}
});
}
Node application:
function applicationStatus(request: Request, response: Response){
try {
var dataResponse = Date.now();
return response.json({resposta: dataResponse })
} catch (error) {
return response.json({resposta: error})
}
}
routes.get('/tempo', applicationStatus);
How to return: Method: 1605634553413 | Server: 1605635103030
But when I subtract the values and make the conversion to seconds gives me an approximate return of 550 seconds, and it does not take even 1 second to have the answer.
Wouldn’t this difference in values be because of the synchronization of the server and the user’s browser? Because Voce is using the
Date.now()
of each environment and then probably they are not synchronized. I’m not sure it can be that. To deduce why Voce placed a url that is not local (localhost), so it is a remote server– Cmte Cardeal
That was exactly the problem. The server is 10 minutes ahead of the application time. Thanks
– Igor Carreiro
Hello Igor Carreiro, the server Timezone is different from the local Timezone (browser). Brazil uses -3 hours in most states (depending on daylight saving time), while the server usually uses "zero zone" (UTC): https://answall.com/a/417492/3635
– Guilherme Nascimento