From 185e07e5802032b7466d08ab76a65c66fe803b77 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 12 Jul 2019 14:21:05 -0400 Subject: [PATCH] Add handling for empty and missing log directory --- src/server.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/server.js b/src/server.js index a1afe8d..9c60bf1 100644 --- a/src/server.js +++ b/src/server.js @@ -30,6 +30,8 @@ client.on("data", function(data) { if (!packet.startsWith("#")) { // ignore comments let date = new Date(); + // create log dir if it doesn't exist + if (!fs.existsSync("log")) fs.mkdirSync("log"); fs.appendFile( `log/log${datestamp(date)}.json`, JSON.stringify([date, packet]) + "\n", @@ -44,8 +46,12 @@ client.on("data", function(data) { wss.on("connection", ws => { let date = new Date(); - fs.readFileSync(`log/log${datestamp(date)}.json`) - .toString() - .split("\n") - .forEach(line => ws.send(line)); + let filename = `log/log${datestamp(date)}.json`; + + if (fs.existsSync(filename)) { + fs.readFileSync(filename) + .toString() + .split("\n") + .forEach(line => ws.send(line)); + } });