From b413427a370a0ced901bf4a9e025508900285dea Mon Sep 17 00:00:00 2001 From: LouisLam Date: Tue, 10 Aug 2021 21:28:54 +0800 Subject: [PATCH] graceful shutdown when listen error --- server/server.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/server/server.js b/server/server.js index 54bbfa4f..9eeca320 100644 --- a/server/server.js +++ b/server/server.js @@ -570,15 +570,19 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); - console.log("Init") - server.listen(port, hostname, () => { + console.log("Init the server") + server.once("error", async (err) => { + console.error("Cannot listen: " + err.message); + await Database.close(); + }); + + server.listen(port, hostname, () => { if (hostname) { console.log(`Listening on ${hostname}:${port}`); } else { console.log("Listening on ${port}"); } - startMonitors(); });