From e66ffc722fe58af2da3d6e3302b134c52ea78569 Mon Sep 17 00:00:00 2001 From: davidovski Date: Thu, 8 Jul 2021 19:34:39 +0100 Subject: added try catch --- main.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/main.js b/main.js index 160248c..ba3da62 100644 --- a/main.js +++ b/main.js @@ -11,25 +11,28 @@ var log = [] // Initialise a simple http server to serve any static files http.createServer((req, res) => { - - // normalise path name to stay within a normal directory - var requestUrl = url.parse(req.url); - var fsPath = static_content + path.normalize(requestUrl.pathname); - - // automatically return the index of a directory, if its been selected - if (fs.statSync(fsPath).isDirectory()) - fsPath += fsPath.endsWith("/") ? "index.html" : "/index.html"; - - // read and send the file - fs.readFile(fsPath, (err, data) => { - if (err) { - res.writeHead(404); - res.end(JSON.stringify(err)) - } else { - res.writeHead(200); - res.end(data); - } - }); + try { + // normalise path name to stay within a normal directory + var requestUrl = url.parse(req.url); + var fsPath = static_content + path.normalize(requestUrl.pathname); + + // automatically return the index of a directory, if its been selected + if (fs.statSync(fsPath).isDirectory()) + fsPath += fsPath.endsWith("/") ? "index.html" : "/index.html"; + + // read and send the file + fs.readFile(fsPath, (err, data) => { + if (err) { + res.writeHead(404); + res.end(JSON.stringify(err)) + } else { + res.writeHead(200); + res.end(data); + } + }); + } catch (ex) { + console.log(ex) + } }).listen(8080); // create websocket: client tracking allows us to get a set of clients so that we can send messages to each client once they appear -- cgit v1.2.1