-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
133 lines (102 loc) · 4.16 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const express = require("express");
const fs = require("fs");
const app = express();
// Parse the request body, earlier, we used to do body-parser.json() but latest versions of express contains this functionality so we can use express.json()
app.use(express.json());
// Test the app to check it works
/* app.get("/", (req, res) => {
const output = { value: "hello world!" }
res.send(output);
}) */
// Utility functions to read and write from file
// Write data to todos.json
const saveTodosData = (data) => {
const stringifyData = JSON.stringify(data);
fs.writeFileSync('todos.json', stringifyData);
}
// Read data from todos.json
const getTodosData = () => {
const jsonData = fs.readFileSync('todos.json');
return JSON.parse(jsonData);
}
// END utility functions
// Read all ToDos --- GET Route
app.get('/todo/list', (re, res) => {
const todos = getTodosData();
console.log(todos);
res.send(todos);
})
// CREATE a ToDo task --- POST Route
app.post('/todo/add', (req, res) => {
// Get the existing ToDos so that we don't loose the current data
const existingTodos = getTodosData();
console.log(`existingTodos: ${existingTodos}`);
//Get the new todo task from the POST request object.
console.log(`req: ${req}`);
const todoData = req.body;
console.log(`todoData: ${JSON.stringify(todoData)}`);
// Incoming data validation
if(todoData.title === null || todoData.description === null) {
return res.status(401).send({error: true, msg: 'Todo Data missing'});
}
// Check if the todo exists already - TODO This functionality needs to be implemented
// Update the array by adding the new ToDo
existingTodos.push(todoData);
console.log(`Updated existingTodos: ${JSON.stringify(existingTodos)}`);
// Save the new Todo data
saveTodosData(existingTodos);
// Redirect to the page to show the updated Todos
console.log("Todo added successfully");
res.send({success: true, msg: 'Todo data added successfully'});
})
// Update a Todo task --- PUT Route
app.put("/todo/update", (req, res) => {
// Get the existing todo data
const existingTodos = getTodosData();
console.log(`existingTodos: ${JSON.stringify(existingTodos)}`);
//Get the new todo task from the POST request object.
console.log(`req: ${req}`);
const todoData = req.body;
console.log(`todoData: ${JSON.stringify(todoData)}`);
// Incoming data validation
if(todoData.title === null || todoData.description === null) {
return res.status(401).send({error: true, msg: 'Todo Data missing'});
}
// Check whether the request Todo exists in Todos.JSON
existingTodos.find(todo => {
if(todo.title === todoData.title) {
todo.description = todoData.description;
}
})
console.log(JSON.stringify(existingTodos));
// Save the new todos to the JSON file
saveTodosData(existingTodos);
console.log('Successfully updated Todo');
res.send({success: true, msg: 'Todo data updated successfully'});
})
// Delete a Todo task --- DELETE Route
app.delete("/todo/delete", (req, res) => {
// Get the existing todo data
const existingTodos = getTodosData();
console.log(`existingTodos: ${JSON.stringify(existingTodos)}`);
//Get the new todo task from the POST request object.
console.log(`req: ${req}`);
const todoData = req.body;
console.log(`todoData: ${JSON.stringify(todoData)}`);
// Incoming data validation
if(todoData.title === null || todoData.description === null) {
return res.status(401).send({error: true, msg: 'Todo Data missing'});
}
// Filter the todo which you don't want to delete
const newListOfTodos = existingTodos.filter(todo => todo.title !== todoData.title)
console.log(`newListOfTodos: ${JSON.stringify(newListOfTodos)}`);
// Save the new todos to the JSON file
saveTodosData(newListOfTodos);
//console.log('Successfully updated Todo');
res.send({success: true, msg: 'Todo data deleted successfully'});
})
// Configure server PORT and start server
const PORT = process.env.PORT || 8001;
app.listen(PORT, () => {
console.log('listening to 8001');
})