-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (57 loc) · 1.97 KB
/
server.js
File metadata and controls
64 lines (57 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Import express, fs and path
const express = require("express");
const fs = require("fs");
const path = require("path");
const dbPath = path.join(__dirname, "db/db.json");
// Configure express server
const app = express();
const PORT = process.env.PORT || 8080;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
// Function to update the db file. Takes one parameter, update,
// which is a function that takes the saved notes as a parameter and returns
// the updated notes to save
function updateDb(res, update) {
fs.readFile(dbPath, "utf8", (err, data) => {
if (send500(err, res)) {
fs.writeFile(dbPath, JSON.stringify(update(JSON.parse(data))), err => send500(err, res));
}
});
}
// Checks if there is an error and handles it. Responds with 500 if there is an error
// Returns true if there is no error and false if there is
function send500(err, res) {
if (err) {
console.error(err);
if (!res.headersSent) { res.sendStatus(500); }
res.end();
return false;
}
return true;
}
// Set up API routes
app.get("/api/notes", (req, res) => res.sendFile(dbPath, err => send500(err, res)));
app.post("/api/notes", (req, res) => updateDb(res, notes => {
const ids = notes.map(note => parseInt(note.id));
let newId = 1;
for (; ids.includes(newId); newId++);
const newNote = { ...req.body, id: newId };
res.json(newNote);
notes.push(newNote);
return notes;
}));
app.delete("/api/notes/:id", (req, res) => {
updateDb(res, notes => notes.filter(note => note.id != req.params.id));
res.end();
});
// Set up HTML GET routes
app.get("/notes", (req, res) => res.sendFile(path.join(__dirname, "public/notes.html")));
app.get("/*", (req, res) => res.sendFile(path.join(__dirname, "public/index.html")));
// Start the server
app.listen(PORT, err => {
if (err) {
console.error(err);
}
console.log(`App listening on port ${PORT}`);
});