-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
86 lines (66 loc) · 2.13 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
83
84
85
86
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
app.use(express.static(path.join(__dirname)));
app.use(bodyParser.json()); // to support JSON body
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
//////INDEX
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname ,'static/index.html'));
})
app.get('/about', function (req, res) {
res.sendFile(path.join(__dirname, 'static/about.html'));
})
app.get('/test', function (req, res) {
res.sendFile(path.join(__dirname, 'jade/test.html'));
})
/////// ENDPOINTS
/////Updates Number of total Wins
app.post('/updatewins', function(req,res){
console.log("updated in database", req.body.userid);
const userid = req.body.userid;
const query = 'update User set gamesWon=gamesWon +1, ranking=ranking +3'+
' where userId='+userid+';';
const mysql = require('mysql')
const connection = mysql.createConnection({
host : 'us-cdbr-iron-east-05.cleardb.net',
user : 'b7cfe3e5b978a2',
password : '1b99fff3',
database : 'heroku_6702d303f45788f'
});
connection.connect();
connection.query(query, function (err, rows, fields) {
connection.destroy();
if (err) throw err;
console.log(rows);
res.send(rows);
})
})
////Get list of users
app.get('/getinitialstate', function (req, res) {
const query='select userId userid,u.name name, u.nickname,rt.name rankingName, '+
'u.ranking rankingNumber,gamesWon '+
'from User u '+
'join RankingType rt '+
'where u.ranking between floor and ceil; '
const mysql = require('mysql')
const connection = mysql.createConnection({
host : 'us-cdbr-iron-east-05.cleardb.net',
user : 'b7cfe3e5b978a2',
password : '1b99fff3',
database : 'heroku_6702d303f45788f'
});
connection.connect();
connection.query(query, function (err, rows, fields) {
connection.destroy();
if (err) throw err;
res.send(rows);
})
})
/// SERVER START WITH node serve.js
app.listen(process.env.PORT || 3000, function () {
console.log('Magic app Started!')
})