-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
152 lines (133 loc) · 4.54 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*//update title of tab
Take Quiz, modify quiz, edit quiz, delete quiz
use templating for dropdown quiz selection?
*/
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var path = require('path');
var path = require('ejs');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var content = fs.readFileSync("static/index.html", 'utf8');
app.use("/static", express.static('static'));
app.set('view engine', 'ejs');
app.get('/', function (req, res) {
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
var titles = [];
for (var i = 0; i<jsonContent.length; i++) {
titles[i] = jsonContent[i]["title"];
}
res.render('index',{titles: titles});
});
app.get('/,/quiz', function (req, res) {
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
var titles = [];
for (var i = 0; i<jsonContent.length; i++) {
titles[i] = jsonContent[i]["title"];
}
res.send(JSON.stringify(titles));
});
app.post('/quiz', function(req, res){
var sentQuiz = req.body;
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
if (jsonContent.length > 0) {
sentQuiz["id"] = jsonContent[jsonContent.length-1]["id"] + 1;
}
jsonContent.push(sentQuiz);
var jsonString = JSON.stringify(jsonContent);
fs.writeFile("data/allQuizzes.json", jsonString);
res.send("updated");
});
app.get('/quiz/:id', function (req, res) {
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
var targetQuiz;;
for (var i = 0; i < jsonContent.length; i++) {
if (jsonContent[i]["id"] === parseInt(req.params.id)) {
targetQuiz = jsonContent[i];
break;
}
}
res.send(targetQuiz);
});
app.put('/quiz/:id', function (req, res) {
var sentQuiz = req.body;
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
for (var i = 0; i < jsonContent.length; i++) {
if (jsonContent[i]["id"] === parseInt(req.params.id)) {
jsonContent[i] = sentQuiz;
break;
}
}
var jsonString = JSON.stringify(jsonContent);
fs.writeFile("data/allQuizzes.json", jsonString);
res.send("updated");
});
app.delete('/quiz/:id', function (req, res) {
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
for (var i = 0; i < jsonContent.length; i++) {
if (jsonContent[i]["id"] === parseInt(req.params.id)) {
jsonContent.splice(i, 1);
break;
}
}
var jsonString = JSON.stringify(jsonContent);
fs.writeFile("data/allQuizzes.json", jsonString);
res.send("deleted");
});
app.get('/reset', function (req, res) {
var readIn = fs.readFileSync("data/defaultallquizzes.json", 'utf8');
// var readInAdded = fs.readFileSync("data/allQuizzes.json", 'utf8');
// fs.writeFile("data/allQuizzesRevert.json", readInAdded);
fs.writeFile("data/allQuizzes.json", readIn);
res.send("default quizzes restored");
});
app.get('/revert', function (req, res) {
var readIn = fs.readFileSync("data/allQuizzesRevert.json", 'utf8');
fs.writeFile("data/allQuizzes.json", readIn);
res.send("reverted");
});
app.get('/users', function (req, res) {
var readUsers = fs.readFileSync("data/users.json", 'utf8');
res.send(readUsers);
});
app.post('/users', function(req, res){
var jsonString = JSON.stringify(req.body);
fs.writeFile("data/users.json", jsonString);
res.send(req.body);
});
app.get('/titles', function (req, res) {
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
var titles = "[";
for (var i = 0; i<jsonContent.length; i++) {
if (i < jsonContent.length -1)
titles += "\"" + jsonContent[i]["title"] + "\"" + ", ";
else
titles += "\"" + jsonContent[i]["title"] + "\"";
}
titles += "]";
res.send(titles);
});
app.get('/titlesandids', function (req, res) {
var readQuiz = fs.readFileSync("data/allQuizzes.json", 'utf8');
var jsonContent = JSON.parse(readQuiz);
var titles = [];
for (var i = 0; i<jsonContent.length; i++) {
titles[i] = jsonContent[i]["title"];
titles[jsonContent.length + i] = jsonContent[i]["id"];
}
res.send(JSON.stringify(titles));
});
var server = app.listen(process.env.PORT || 4000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});