-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
78 lines (67 loc) · 1.74 KB
/
app.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
var express = require('express');
var expressHandlebars = require('express-handlebars');
var http = require('http');
var redis = require('redis');
var bodyParser = require('body-parser');
var async = require('async');
var PORT = 8000;
var urlencodedParser = bodyParser.urlencoded({ extended: true });
var client = redis.createClient({
'host': 'redis'
});
var app = express();
app.engine('html', expressHandlebars());
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static('css'));
app.get('/', function(req, res) {
res.render('index', {rv: client.server_info.redis_version});
});
app.get('/results', function(req, res) {
getVotes(function(results){
res.render('results', results);
});
});
//render results page======================================
app.post('/results', urlencodedParser, function (req, res) {
if (!req.body) return res.sendStatus(400)
vote(req.body.tech, function(){
getVotes(function(result){
res.render('results', result);
});
});
});
var getVotes = function (done){
var tmp = ['node','java','dotnet'];
var result = {count:0};
async.each(tmp, function(name, cb){
client.get(name, function(err, reply){
if(!err && reply){
result[name] = parseInt(reply, 10);
result.count += result[name];
//if(name != 'node')
// result[name] = 0;
}
else
result[name] = 0;
cb();
});
}, function() {
done(result);
});
};
var vote = function (key, done ){
var count = 0;
client.get(key, function(err, reply){
if (!err && reply) count = reply;
count++;
client.set(key, count);
done(count);
});
};
http.Server(app).listen(PORT, function() {
console.log("HTTP server listening on port %s", PORT);
});
process.on('SIGTERM', function () {
process.exit(0);
});