Verificar se servidor já está rodando

This commit is contained in:
Luiz Silva 2024-08-12 14:00:52 -03:00
parent 2c7404443a
commit f8adae2f47

View file

@ -143,10 +143,19 @@ gerar.then(() => {
.send(html404.replace("{ERRO}", `Página não encontrada: ${request.url}`)); .send(html404.replace("{ERRO}", `Página não encontrada: ${request.url}`));
}); });
const jaEstaRodando = fetch(`http://127.0.0.1:${PORTA}`)
.then(() => true)
.catch(() => false);
jaEstaRodando.then((jaEstaRodando) => {
!jaEstaRodando &&
fastify.listen({ port: Number(PORTA) }, (err, address) => { fastify.listen({ port: Number(PORTA) }, (err, address) => {
if (err) throw err; if (err) throw err;
console.log( console.log(
`${new Date().toISOString()} Servidor ${PREFIXO} Rodando em ${address}`, `${new Date().toISOString()} Servidor ${PREFIXO} Rodando em ${address}`,
); );
}); });
jaEstaRodando && console.log(`Servidor ${PREFIXO} já está rodando`);
});
}); });