-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (85 loc) · 2.65 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
87
88
89
90
91
92
93
94
95
96
97
98
const mysql = require('mysql')
const express = require('express')
const app = express()
app.use(express.static('public'))
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
extended: true
}));
function escapeCharacters(string){
return string.replace(/&/g, '&').replace(/</g, '<').replace(/>/g,'>').replace(/\//g,'/');
};
app.use(bodyParser.json());
var pool = mysql.createPool({
connectionLimit: 100,
host: "MYHOST",
user: "NAME",
password: "PASS",
database: "DB"
});
app.get('/', function (req, res) {
res.sendFile(__dirname + "/index.html");
})
app.get('/card', function (req, res) {
pool.getConnection(function(err,connection) {
if (err) throw err;
connection.query("select * from (select * from cards where score > -5 order by RAND() LIMIT 3) as topitems order by score desc LIMIT 1",function (err, result, fields) {
connection.release();
if (err) throw err;
res.send(result[0]);
});
/*
connection.query("select * from (select * from cards order by score DESC LIMIT 100) as topitems order by RAND() LIMIT 1",function (err, result, fields) {
connection.release();
if (err) throw err;
res.send(result[0]);
});
*/
});
})
app.post('/card', function (req, res) {
//console.log(req.body);
pool.getConnection(function(err,connection) {
if (err) throw err;
var scenario = escapeCharacters(req.body.scenario);
var title = escapeCharacters(req.body.title);
var sql ="INSERT INTO cards (scenario, title, score) VALUES ?";
var values = [[scenario,title,1]];
connection.query(sql,[values], function (err, result, fields) {
if (err) throw err;
});
connection.query("SELECT * FROM cards", function (err, result, fields) {
connection.release();
if (err) throw err;
res.sendFile(__dirname + "/public/index.html");
});
});
});
app.post('/upvote',function(req,res){
console.log(req);
pool.getConnection(function(err,connection) {
if (err) throw err;
var sql ="UPDATE cards SET score = score + 1 WHERE cardID = ?"
var values = [[req.body.cardID]];
connection.query(sql,[values], function (err, result, fields) {
connection.release();
if (err) throw err;
res.sendFile(__dirname + "/public/index.html");
});
});
});
app.post('/downvote',function(req,res){
console.log(req.body);
pool.getConnection(function(err,connection) {
if (err) throw err;
var sql ="UPDATE cards SET score = score - 1 WHERE cardID = ?"
var values = [[req.body.cardID]];
connection.query(sql,[values], function (err, result, fields) {
connection.release();
if (err) throw err;
console.log(result);
res.sendFile(__dirname + "/public/index.html");
});
});
});
app.listen(80,'0.0.0.0');