Fix "file not found"

This commit is contained in:
Aelita4 2022-08-17 10:19:46 +02:00
parent 60cc43be98
commit 7e25c2162e
Signed by: Aelita4
GPG Key ID: F8EC95519509D1D5
3 changed files with 5 additions and 1 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules/ node_modules/
src/data.json

View File

@ -1 +0,0 @@
["127.0.0.1","213.180.141.140","192.168.23.1","1.1.1.1"]

View File

@ -13,6 +13,7 @@ const ping = async host => {
const app = new express(); const app = new express();
app.get("/", async (req, res) => { 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 addresses = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}))
const timeoutDelay = 60000; const timeoutDelay = 60000;
@ -56,6 +57,7 @@ app.get("/", async (req, res) => {
document.getElementById("eta").innerHTML = "Next scheduled check: " + new Date(date.getTime() + ${timeoutDelay}); document.getElementById("eta").innerHTML = "Next scheduled check: " + new Date(date.getTime() + ${timeoutDelay});
addresses.forEach(async a => { addresses.forEach(async a => {
const b = document.getElementById(a + "_status"); const b = document.getElementById(a + "_status");
if(!b) return;
b.innerHTML = '<span style="color:blue;">PINGING...</span>' b.innerHTML = '<span style="color:blue;">PINGING...</span>'
await fetch('http://localhost:8080/ping/' + a) await fetch('http://localhost:8080/ping/' + a)
.then((response) => response.json()) .then((response) => response.json())
@ -100,6 +102,7 @@ app.get("/ping/:ip", async (req, res) => {
}); });
app.get("/addIP/: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'})); const json = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}));
json.push(req.params.ip); json.push(req.params.ip);
@ -112,6 +115,7 @@ app.get("/addIP/:ip", async (req, res) => {
}); });
app.get("/removeIP/: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'})); const json = JSON.parse(fs.readFileSync("./data.json", {encoding:'utf8', flag:'r'}));
json.splice(json.indexOf(req.params.ip.toString()), 1) json.splice(json.indexOf(req.params.ip.toString()), 1)