-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
105 lines (72 loc) · 2.5 KB
/
server.js
File metadata and controls
105 lines (72 loc) · 2.5 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
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
103
104
105
const express = require("express");
const app = express();
const notes = require("./NotesDB.json");
// file system module to perform file operations
// const fs = require('fs');
// // json data
// var jsonData = '{"persons":[{"name":"John","city":"New York"},{"name":"Phil","city":"Ohio"}]}';
// // parse json
// var jsonObj = JSON.parse(jsonData);
// console.log(jsonObj);
// // stringify JSON Object
// var jsonContent = JSON.stringify(jsonObj);
// console.log(jsonContent);
// fs.writeFile("output.json", jsonContent, 'utf8', function (err) {
// if (err) {
// console.log("An error occured while writing JSON Object to File.");
// return console.log(err);
// }
// console.log("JSON file has been saved.");
// });
// const connectRouter = require("./routers/connection");
// app.use("/connectReact", connectRouter);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// GET - Request for All Notes
app.get("/notes", (req, res) => {
res.json(notes);
console.log(notes);
});
// GET - Request for a specific ID
app.get("/notes/:id", (req, res) => {
res.json(notes.filter(
(note) => note.id === parseInt(req.params.id)
));
});
// POST - Request to Add a new Note
app.post("/notes", (req, res) => {
const id_num = Math.floor(Math.random() * 10000) + 1;
const newNote = {
"id": id_num,
"text": req.body.text,
"day": req.body.day,
"reminder": req.body.reminder
}
notes.push(newNote);
res.json(notes);
});
// PUT - Request to Update Reminder Status of a specific ID
app.put("/notes/:id", (req, res) => {
const found = notes.some((note) => note.id === parseInt(req.params.id));
if (found) {
notes.forEach(
(note) => {
if (note.id === parseInt(req.params.id)) {
note.reminder = req.body.reminder;
res.json({"message" : "Note Updated", note , "All Data" : notes});
}
});
}
});
// DELETE - Request to Delete a Note
app.delete("/notes/:id", (req, res) => {
const found = notes.some((note) => note.id === parseInt(req.params.id));
const leftData = notes.filter((note) => note.id !== parseInt(req.params.id));
if (found)
{
res.json({"message" : "Deleted Member", "Remaing Data" : leftData});
}
// notes.push(leftData);
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server UP!! Listening at PORT ${PORT}`));