Cleaned up server.js a bit
This commit is contained in:
parent
f860fceeb6
commit
99dcd64820
16
server.js
16
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user