From 99dcd648200ec32e33f43e09e75e466f8d614f3b Mon Sep 17 00:00:00 2001 From: Thomas Wade Date: Mon, 29 Oct 2018 18:33:32 +1030 Subject: [PATCH] Cleaned up server.js a bit --- server.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index 5fc6dea..f46c844 100644 --- a/server.js +++ b/server.js @@ -13,17 +13,20 @@ app.use(express.json()); // Parse JSON in requests // }); // Root route, respond with with index.html -app.get("/", function(req, res) { +app.get("/", function(req, res) +{ res.sendFile("./index.html"); }); // Posts route, return posts.json -app.get("/posts", function(req, res) { +app.get("/posts", function(req, res) +{ res.json(JSON.parse(fs.readFileSync('./data/posts.json'))); }); // Posts route, digest received post -app.post("/posts", function(req, res) { +app.post("/posts", function(req, res) +{ var postsFile = fs.readFileSync('./data/posts.json'); var posts = JSON.parse(postsFile); @@ -38,7 +41,8 @@ app.post("/posts", function(req, res) { return; } - fs.writeFile('./data/posts.json', JSON.stringify(posts), (e) => { + fs.writeFile('./data/posts.json', JSON.stringify(posts), (e) => + { if (e) { res.sendStatus(500); @@ -50,10 +54,12 @@ app.post("/posts", function(req, res) { }); // Start listening -app.listen(8080, function (){ +app.listen(8080, function() +{ console.log("Listening on port 8080"); }); +// Validates a post object conforms to the standard we're expecting function validatePost(post) { try