forked from constructorlabs/quiz-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
134 lines (116 loc) · 3.49 KB
/
server.js
File metadata and controls
134 lines (116 loc) · 3.49 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
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
const express = require('express');
const app = express();
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
const storage = {
counter: 0,
currentQuestion: null,
level: "easy",
}
let answers = {
1: false,
2: false,
3: true,
4: false
}
app.use(bodyParser.json());
app.set('view engine', 'hbs');
app.use('/static', express.static('static'));
app.get('/', function(req, res) {
res.render('first-page', req.body);
});
// get on initial load
app.get('/quiz', function(req,res) {
fetch('https://opentdb.com/api.php?amount=1&difficulty=easy&type=multiple&category=18')
.then(function(response){
return response.json();
}).then(function(json){
let data = json.results[0];
res.render('mainPage', data);
}).catch(function(error){
res.status(500).json({ error: 'We failed to fetch the question' });
});
});
// get when fetch call from client
app.get('/check-answer/:answer', function(req, res) {
let answer = req.params.answer;
if (answer === "correct") {
storage.counter += 1;
if (storage.counter < 10) {
storage.level = 'easy';
} else if (storage.counter >= 10 && storage.counter < 20) {
storage.level = 'medium';
} else if (storage.counter >= 20) {
storage.level = 'hard';
}
//console.log(storage.level);
// response to client with counter
//fetch new question from API
fetch(`https://opentdb.com/api.php?amount=1&difficulty=${storage.level}&type=multiple&category=18`)
.then(function(response){
return response.json();
}).then(function(json){
let data = json.results[0];
data.counter = storage.counter;
data.level = storage.level;
res.json(data);
}).catch(function(error){
res.status(500).json({ error: 'We failed to fetch the question' });
});
} else if (answer === "wrong") {
storage.counter -= 1;
if (storage.counter < 0){
storage.counter = 0;
}
if (storage.counter < 10) {
storage.level = 'easy';
} else if (storage.counter >= 10 && storage.counter < 20) {
storage.level = 'medium';
} else if (storage.counter >= 20) {
storage.level = 'hard';
}
fetch(`https://opentdb.com/api.php?amount=1&difficulty=${storage.level}&type=multiple&category=18`)
.then(function(response){
return response.json();
}).then(function(json){
let data = json.results[0];
data.counter = storage.counter;
data.level = storage.level;
res.json(data);
}).catch(function(error){
res.status(500).json({ error: 'We failed to fetch the question' });
});
}
})
app.get('/get-question', function(req, res) {
if (storage.currentQuestion === null) {
fetch('https://opentdb.com/api.php?amount=1&type=multiple')
.then(function(response){
return response.json();
}).then(function(json){
//res.json(data.results[0]);
let data = json.results[0];
res.render('mainPage', data);
});
}
else {
//res.json(storage.currentQuestion);
res.render(storage.currentQuestion);
}
});
app.listen(8080, function(){
console.log('I am listening on port 8080');
});
/// Maybe no longer needed
app.get('/temp', function(req, res) {
fetch('https://opentdb.com/api.php?amount=1')
.then(function(response){
return response.json();
}).then(function(json){
let data = json.results[0];
data.counter = storage.counter
res.render('quizViewer', data);
}).catch(function(error){
res.status(500).json({ error: 'We failed to fetch the question' });
});
});