-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (59 loc) · 1.45 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
// load all env variables from .env file into process.env object.
require('dotenv').config()
var express = require('express');
var cors = require('cors');
const path = require("path")
var app = express();
app.use(cors());
app.use(express.json())
const PORT = process.env.PORT || 3000
const Pool = require('pg').Pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
})
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
)
next()
})
async function addVote(req) {
let result = req.body
await pool.query(`INSERT INTO votes (seed, timestamp, timezone) VALUES($1,$2,$3)`, [result.seed, result.timestamp, result.timezone],
(err, res) => {
if (err) {
throw err
}
}
);
}
function getAllVotes(res) {
pool.query('SELECT * FROM votes', (err, results) => {
if (err) {
throw err
}
res.send(results.rows)
})
}
app.post('/api/add_vote', async function (req, res) {
await addVote(req)
});
app.get('/api/votes', function (req, res) {
getAllVotes(res)
});
app.use(express.static(path.join(__dirname, "/dist")))
app.listen(PORT, "0.0.0.0", function onStart(err) {
if (err) {
console.log(err)
}
console.info(
"==> 🌎 Listening on port %s. Open up http://0.0.0.0:%s/ in your browser.",
PORT,
PORT
)
})