Add handling for empty and missing log directory
This commit is contained in:
parent
0b301b32f6
commit
185e07e580
@ -30,6 +30,8 @@ client.on("data", function(data) {
|
|||||||
if (!packet.startsWith("#")) {
|
if (!packet.startsWith("#")) {
|
||||||
// ignore comments
|
// ignore comments
|
||||||
let date = new Date();
|
let date = new Date();
|
||||||
|
// create log dir if it doesn't exist
|
||||||
|
if (!fs.existsSync("log")) fs.mkdirSync("log");
|
||||||
fs.appendFile(
|
fs.appendFile(
|
||||||
`log/log${datestamp(date)}.json`,
|
`log/log${datestamp(date)}.json`,
|
||||||
JSON.stringify([date, packet]) + "\n",
|
JSON.stringify([date, packet]) + "\n",
|
||||||
@ -44,8 +46,12 @@ client.on("data", function(data) {
|
|||||||
|
|
||||||
wss.on("connection", ws => {
|
wss.on("connection", ws => {
|
||||||
let date = new Date();
|
let date = new Date();
|
||||||
fs.readFileSync(`log/log${datestamp(date)}.json`)
|
let filename = `log/log${datestamp(date)}.json`;
|
||||||
.toString()
|
|
||||||
.split("\n")
|
if (fs.existsSync(filename)) {
|
||||||
.forEach(line => ws.send(line));
|
fs.readFileSync(filename)
|
||||||
|
.toString()
|
||||||
|
.split("\n")
|
||||||
|
.forEach(line => ws.send(line));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user