summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@sendula.com>2021-07-08 19:34:39 +0100
committerdavidovski <david@sendula.com>2021-07-08 19:34:39 +0100
commite66ffc722fe58af2da3d6e3302b134c52ea78569 (patch)
treeaab9e51b5fcd9b44feeb85e48ba971f73d28699c
parent6a3960283ab36711bf184a911e592cf8c63dec07 (diff)
added try catch
-rw-r--r--main.js41
1 files 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