-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
82 lines (71 loc) · 2.21 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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const mysql = require('mysql');
const config = require('./config');
const app = express();
const port = config.appPort || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const connection = mysql.createConnection({
host: config.host,
user: config.user,
password: config.password,
database: config.database,
port: config.port
});
connection.connect();
// API calls
app.get('/api/game/:gameId', (req, res) => {
const gameId = req.params.gameId;
connection.query(`SELECT * FROM games WHERE id = ${gameId};`, function (error, results) {
if (error) throw error;
res.send({ game: results[0] });
});
});
app.get('/api/game/:gameId/questions', (req, res) => {
const gameId = req.params.gameId;
connection.query(
`SELECT q.* FROM questions q JOIN games g ON q.game_id = g.id WHERE g.id = ${gameId};`,
function (error, results) {
if (error) throw error;
res.send({ questions: results });
});
});
app.get('/api/tokens', (req, res) => {
connection.query('SELECT * FROM tokens;', function (error, results) {
if (error) throw error;
res.send({ tokens: results });
});
});
app.get('/api/game/:gameId/question_tokens', (req, res) => {
const gameId = req.params.gameId;
connection.query(
`
SELECT * FROM question_tokens qt
JOIN questions q ON qt.question_id = q.id
WHERE q.id IN
(
SELECT q.id
FROM questions q
JOIN games g ON q.game_id = g.id
WHERE g.id = ${gameId}
);
`, function (error, results) {
if (error) throw error;
res.send({ questionTokens: results });
});
});
if (config.nodeEnv === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
app.listen(port, () => console.log(`Listening on port ${port}`)); // eslint-disable-line no-console
process.on('SIGINT', function () {
connection.end();
process.exit();
});