-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (50 loc) · 1.32 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
var express = require("express"),
bodyParser = require("body-parser")
facts = require("./facts.json"),
quiz = require("./quiz.json")
app = express();
app.use(bodyParser.urlencoded({extended: true}));
const PORT = process.env.PORT || 5000
app.listen(PORT,function(){
console.log("API server online");
})
app.get("/",function(req,res){
res.render("home.ejs")
})
app.get("/quiz",function(req,res){
res.json(quiz_q(1));
})
app.get("/quiz/num=:quantity",function(req,res){
res.json(quiz_q(req.params.quantity));
})
app.get("/facts",function(req,res){
res.json(fact_q(1));
})
app.get("/facts/num=:quantity",function(req,res){
res.json(fact_q(req.params.quantity));
})
app.get("*",function(req,res){
res.send("<h1>Error404<h1><h3>Lost like Payet :/<h3>");
})
function fact_q(quantity){
var factarray=[];
for(var i=0;i<quantity;i++)
{
var randomvar=Math.random();
randomvar*=facts.length;
randomvar=Math.floor(randomvar)
factarray.push(facts[randomvar]);
}
return factarray;
}
function quiz_q(quantity){
var quizarray=[];
for(var i=0;i<quantity;i++)
{
var randomvar=Math.random();
randomvar*=quiz.length;
randomvar=Math.floor(randomvar)
quizarray.push(quiz[randomvar]);
}
return quizarray;
}