-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
102 lines (87 loc) · 2.95 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Express import
const express = require("express");
// File system module import
const fs = require("fs");
// Path import
const path = require("path");
// Helper method for generating unique ids
const uniqid = require("uniqid");
// Port
const PORT = process.env.PORT || 3001;
// Creates new app with express
const app = express();
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("Develop/public"));
// Get route which sends back the index.html page
app.get("/", (req, res) =>
res.sendFile(path.join(__dirname, "Develop/public/index.html"))
);
// Get route which sends back the notes.html page
app.get("/notes", (req, res) =>
res.sendFile(path.join(__dirname, "Develop/public/notes.html"))
);
// Get route -> which reads the db.json file and sends back the parsed JSON data
app.get("/api/notes", function (req, res) {
fs.readFile("Develop/db/db.json", "utf8", (err, data) => {
var jsonData = JSON.parse(data);
console.log(jsonData);
res.json(jsonData);
});
});
// Reads the newly added notes from the request body and then adds them to the db.json file
const readThenAppendToJson = (content, file) => {
fs.readFile(file, "utf8", (err, data) => {
if (err) {
console.error(err);
} else {
const parsedData = JSON.parse(data);
parsedData.push(content);
writeNewNoteToJson(file, parsedData);
}
});
};
// Writes data to db.json -> utilized within the readThenAppendToJson function
const writeNewNoteToJson = (destination, content) =>
fs.writeFile(destination, JSON.stringify(content, null, 4), (err) =>
err ? console.error(err) : console.info(`\nData written to ${destination}`)
);
// Post route -> receives a new note, saves it to request body, adds it to the db.json file, and then returns the new note to the client
app.post("/api/notes", (req, res) => {
const { title, text } = req.body;
if (title && text) {
const newNote = {
title: title,
text: text,
id: uniqid(),
};
readThenAppendToJson(newNote, "Develop/db/db.json");
const response = {
status: "success",
body: newNote,
};
res.json(response);
} else {
res.json("Error in posting new note");
}
});
// Delete route -> reads the db.json file, uses the json objects uniqids to match the object to be deleted, removes that object from the db.json file, then re-writes the db.json file
app.delete("/api/notes/:id", (req, res) => {
let id = req.params.id;
let parsedData;
fs.readFile("Develop/db/db.json", "utf8", (err, data) => {
if (err) {
console.error(err);
} else {
parsedData = JSON.parse(data);
const filterData = parsedData.filter((note) => note.id !== id);
writeNewNoteToJson("Develop/db/db.json", filterData);
}
});
res.send(`Deleted note with ${req.params.id}`);
});
// App.listen is used to spin up our local server
app.listen(PORT, () =>
console.log(`App listening at http://localhost:${PORT} 🚀`)
);