-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
135 lines (122 loc) · 3.81 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
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
// Connect to Mongo DB
require("./data/user-db")
require("dotenv").config()
// Requiring middleware
const express = require("express")
const exphbs = require("express-handlebars")
const bodyParser = require("body-parser")
const http = require("http")
const axios = require("axios")
const mongoose = require("mongoose")
const cookieParser = require("cookie-parser")
const jwt = require("jsonwebtoken")
const js = require('jservice-node');
// Instantiate express
const app = express()
// Integrating middleware
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.use(express.static('public'))
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(bodyParser.json())
app.use(cookieParser())
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/trivia-binge')
// Connecting controller
const auth = require("./controllers/auth.js")(app)
// Render Homepage: Random Question
app.get("/", (req, res) => {
// Grab one random question from jservice-node
js.random(1, function(error, response, json){
if(!error && response.statusCode == 200){
// console.log(json[0].question);
const question = json[0].question;
const answer = json[0].answer;
const category = json[0].category.title;
let queryString = answer;
let term = encodeURIComponent(queryString)
let url = "http://en.wikipedia.org/w/api.php?action=opensearch&search="+term+"&limit=1&format=json"
// Populate Wiki entry using axios
axios.get(url)
.then(response => {
res.render("home", {
fyiDecript: response.data[2],
fyiLink: response.data[3],
question: question,
answer: answer,
category: category
})
})
.catch(error => {
console.log(error);
});
} else {
console.log(`Error: ${response.statusCode}`);
}
});
})
// Render Homepage: Single Question by ID
app.get("/a/:category/:value", (req, res) => {
const options = {
value: req.params.value,
category: req.params.category,
}
js.clues(options, function(error, response, json){
if(!error && response.statusCode == 200){
// console.log(json);
const question = json[0].question;
const answer = json[0].answer;
const category = json[0].category.title;
let queryString = answer;
let term = encodeURIComponent(queryString)
let url = "http://en.wikipedia.org/w/api.php?action=opensearch&search="+term+"&limit=1&format=json"
// Populate Wiki entry using axios
axios.get(url)
.then(response => {
res.render("home", {
fyiDecript: response.data[2],
fyiLink: response.data[3],
question: question,
answer: answer,
category: category
})
})
.catch(error => {
console.log(error);
});
} else {
console.log(`Error: ${response.statusCode}`);
}
});
})
app.get("/sample", (req, res) => {
js.random(6, function(error, response, json){
if(!error && response.statusCode == 200){
const samples = json
res.render("sample", {samples: samples})
} else {
console.log(`Error: ${response.statusCode}`);
}
});
})
// // Testing category generation
// const gameCategories = []
// app.get("/game/start", (req, res) => {
// var catOptions = {
// count: 5,
// offset: 25
// };
//
// //
// js.categories(catOptions, function(error, response, json){
// if(!error && response.statusCode == 200){
// res.render("start", {categories: json});
// } else {
// console.log(`Error: ${response.statusCode}`);
// }
// });
// })
app.listen(process.env.PORT || 3000, (req, res) => {
console.log("Listening at port 3000!")
})