-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (57 loc) · 1.64 KB
/
app.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
const express = require("express");
const ejs = require("ejs");
const path = require("path");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({
extended:true
}))
app.set('views', path.join(__dirname, 'views'));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
const categories=[];
const ids=[];
https.get("https://opentdb.com/api_category.php",function(response){
response.on("data",function(data){
const result=JSON.parse(data);
result.trivia_categories.forEach(function(item){
categories.push(item.name);
ids.push(item.id)
});
})
})
app.get("/",function(req,res){
res.render("home",{categories:categories,id:ids});
})
app.post("/:id",function(req,res){
var id=req.params.id;
https.get("https://opentdb.com/api.php?amount=10&category="+id.toString(),function(response){
response.on("data",function(data){
const result=JSON.parse(data);
const list=[];
result.results.forEach(function(item){
const questions={
question:item.question,
answers: shuffle(item.incorrect_answers.concat(item.correct_answer)),
correct:item.correct_answer
}
list.push(questions);
});
res.render("start",{list:list});
})
})
})
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i)
const temp = array[i]
array[i] = array[j]
array[j] = temp
}
return array;
}
app.listen(process.env.PORT || 3000, function (req, res) {
console.log("Server started at port 3000");
});