From 7e25c2162e451e84749160c57045d9eec8e89c4e Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Wed, 17 Aug 2022 10:19:46 +0200 Subject: [PATCH] Fix "file not found" --- .gitignore | 1 + src/data.json | 1 - src/index.js | 4 ++++ 3 files changed, 5 insertions(+), 1 deletion(-) delete mode 100644 src/data.json diff --git a/.gitignore b/.gitignore index c2658d7..6d73634 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +src/data.json \ No newline at end of file diff --git a/src/data.json b/src/data.json deleted file mode 100644 index 0c08dc8..0000000 --- a/src/data.json +++ /dev/null @@ -1 +0,0 @@ -["127.0.0.1","213.180.141.140","192.168.23.1","1.1.1.1"] \ No newline at end of file diff --git a/src/index.js b/src/index.js index e7a2aec..d5120d1 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ const ping = async host => { const app = new express(); app.get("/", async (req, res) => { + if(!fs.existsSync("data.json")) fs.writeFileSync("data.json", "[]"); const addresses = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'})) const timeoutDelay = 60000; @@ -56,6 +57,7 @@ app.get("/", async (req, res) => { document.getElementById("eta").innerHTML = "Next scheduled check: " + new Date(date.getTime() + ${timeoutDelay}); addresses.forEach(async a => { const b = document.getElementById(a + "_status"); + if(!b) return; b.innerHTML = 'PINGING...' await fetch('http://localhost:8080/ping/' + a) .then((response) => response.json()) @@ -100,6 +102,7 @@ app.get("/ping/:ip", async (req, res) => { }); app.get("/addIP/:ip", async (req, res) => { + if(!fs.existsSync("data.json")) fs.writeFileSync("data.json", "[]"); const json = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'})); json.push(req.params.ip); @@ -112,6 +115,7 @@ app.get("/addIP/:ip", async (req, res) => { }); app.get("/removeIP/:ip", async (req, res) => { + if(!fs.existsSync("data.json")) fs.writeFileSync("data.json", "[]"); const json = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'})); json.splice(json.indexOf(req.params.ip.toString()), 1)