-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
69 lines (58 loc) · 2.37 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
const express = require("express");
const path = require("path");
const RandomWord = require('random-word');
const PORT = process.env.PORT || 3001;
const app = express();
var connection = require("./connection");
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Define API routes here
app.get("/api/randomword", (req,res) => {
res.json({word: RandomWord()});
});
app.post("/signin", (req, res) => {
// add logic to store user details in DB.
// req.body.googleId, req.body.highScore, etc...
connection.query(`SELECT * FROM users WHERE googleid=${req.body.googleId}`, function (err,result) {
if (err) throw err;
console.log(JSON.stringify(result));
if (!result.length) {
connection.query('INSERT INTO users (name, googleid, highscore) VALUES (?,?,?)', [req.body.name, req.body.googleId, req.body.highScore], function (err,newUser) {
if(err) throw err;
console.log('Successfully saved id and name to db.', newUser);
res.json({success: true, msg: 'User details saved to DB', user: newUser});
});
} else {
console.log("User already exists");
res.json({success: true, msg: 'User already exists', result: result});
}
});
});
app.post("/updatehighscore/:id", (req, res) => {
// connection.query('INSERT INTO users (name, googleid, highscore) VALUES (?,?,?)', [req.body.name, req.body.googleId, req.body.highScore], function (err, result) {
connection.query("UPDATE users SET highscore=? WHERE googleid=?", [req.body.highScore, req.body.googleId], function (err, result) {
if (err) throw err;
console.log('Successfully updated user score.', result);
res.json({success: true, msg: 'User details saved to DB', result});
});
});
app.get("/leaderboard", (req, res) => {
connection.query("SELECT * FROM users ORDER BY highscore DESC", function (error, results) {
if (error) throw error;
console.log("leaderboard", results)
res.json(results)
});
})
// Send every other request to the React app
// Define any API routes before this runs
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.listen(PORT, () => {
console.log(`🌎 ==> API server now on port ${PORT}!`);
});