-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (52 loc) · 1.74 KB
/
Copy pathapp.js
File metadata and controls
67 lines (52 loc) · 1.74 KB
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
var express = require('express');
var http = require('http');
var fs = require('fs');
var app = express();
var server = http.createServer(app);
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
extended: true
}));
var firebase = require('firebase');
// See https://firebase.google.com/docs/web/setup#project_setup for how to
// auto-generate this config
var config = {
apiKey: "AIzaSyDYhRaGOtnjC4JmmgTXag6iBLBlb8MVhQE",
authDomain: "make-xkcd-great-again.firebaseapp.com",
databaseURL: "https://make-xkcd-great-again.firebaseio.com"
};
firebase.initializeApp(config);
var rootRef = firebase.database().ref();
// serve static files
app.use(express.static('client'));
// get all files for the current candidate
app.get('/api/images/:candidate', function(req, res) {
var path = 'client/img/' + req.params.candidate
fs.readdir(path, function(err, items) {
res.send(items)
});
})
// updates Firebase with another vote for the candidate that was voted for
app.post('/api/vote', function(req, res) {
var candidate = req.body.candidate.toLowerCase();
var counter = rootRef.child('counter');
counter.child(candidate).once("value", function(snapshot){
x = snapshot.val();
if (candidate === "trump") {
counter.update({"trump": x+1});
}
else if (candidate === "hillary") {
counter.update({"hillary": x+1});
}
});
});
// have all links direct to our main file
app.all('/*', function ( req, res ) {
res.sendFile(__dirname + '/client/index.html');
})
// make the server start and listen
server.listen(process.env.PORT || 3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Make XKCD Great Again is running on port " + port);
});