-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
127 lines (93 loc) · 2.68 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
var TODO = require('./TODO.js');
var Database = require('./Database.js');
var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
//create a new TODO
if(q.pathname == "/create"){
//currently not validating input for time sake, using big try/catch instead
try{
//create the TODO for the given username and add it to the database
var username = q.query.username;
var due = q.query.due;
var title = q.query.title;
var desc = q.query.desc;
var newTODO = new TODO(due, title, desc);
Database.addTODO(username, newTODO);
//return the successfull TODO
res.writeHead(200);
res.write(JSON.stringify(newTODO));
res.end();
}
catch(error){
//something went wrong, error
res.writeHead(404);
res.write(JSON.stringify(error));
res.end();
}
}
//delete an existing TODO
else if (q.pathname == "/delete"){
//currently not validating input for time sake, using big try/catch instead
try{
//delete the TODO for the given username and id
var username = q.query.username;
var id = q.query.id;
var deleted = Database.deleteTODO(username, id);
//return the successful delete
res.writeHead(200);
res.write(JSON.stringify("Deleted = "+deleted));
res.end();
}
catch(error){
//something went wrong, error
res.writeHead(404);
res.write(JSON.stringify(error));
res.end();
}
}
//update an existing TODO
else if (q.pathname == "/update"){
//currently not validating input for time sake, using big try/catch instead
try{
//update the TODO for the given username and info from query
var username = q.query.username;
var due = q.query.due;
var title = q.query.title;
var desc = q.query.desc;
var complete = q.query.complete;
var id = q.query.id;
var updated = Database.updateTODO(username, id, due, title, desc, complete);
//return the successful update
res.writeHead(200);
res.write(JSON.stringify("Updated = "+updated));
res.end();
}
catch(error){
//something went wrong, error
res.writeHead(404);
res.write(JSON.stringify(error));
res.end();
}
}
//read a user's TODOs
else if (q.pathname == "/read"){
//currently not validating input for time sake, using big try/catch instead
try{
//read the TODOs of a given username from the database, filtering them appropriately
var username = q.query.username;
var list = Database.readTODO(username);
//return the successful list
res.writeHead(200);
res.write(JSON.stringify(list));
res.end();
}
catch(error){
//something went wrong, error
res.writeHead(404);
res.write(JSON.stringify(error));
res.end();
}
}
}).listen(3000);