-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (49 loc) · 1.49 KB
/
server.js
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
// Dependencies
var express = require("express");
var path = require("path");
var fs = require("fs");
// Sets up the Express App
var app = express();
var PORT = process.env.PORT || 3000;
// Sets up the Express app to handle data parsing
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
app.get("/notes", function(req, res) {
res.sendFile(path.join(__dirname, "/public/notes.html"));
});
app.get("/api/notes", function(req, res) {
res.sendFile(path.join(__dirname, "/db/db.json"));
});
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "/public/index.html"));
});
app.post("/api/notes", function(req, res) {
var reqBody = req.body;
var notes = fs.readFileSync("./db/db.json");
reqBody.id = String(notes.length);
notes = JSON.parse(notes);
notes.push(reqBody);
fs.writeFileSync("./db/db.json", JSON.stringify(notes));
res.json(notes);
})
app.delete("/api/notes/:id", function(req, res) {
var noteId = req.params.id;
console.log(noteId);
notes = fs.readFileSync("./db/db.json");
notes = JSON.parse(notes);
notes = notes.filter(function(note) {
if (noteId === note.id ){
return false;
}
else {
return true;
}
})
fs.writeFileSync("./db/db.json", JSON.stringify(notes));
res.json(notes);
});
// Starts the server to begin listening
app.listen(PORT, function() {
console.log("App listening on PORT " + PORT);
});