-
Notifications
You must be signed in to change notification settings - Fork 0
/
increment.js
135 lines (94 loc) · 3.78 KB
/
increment.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Define Canidates */
const candidates = [
{ name: 'Hillary Clinton', color: 'blue' },
{ name: 'Donald Trump', color: 'red' }
];
/* The name of the poll */
var poll = 'elections';
/* Include and setup Increments */
const increments = require('./lib/increments');
/* Increments secure settings */
increments.setup('mongodb://increment:inc@localhost:27017/increment', function (err) {
if (err) throw (err);
console.log('Connecting to Database: mongodb://increment:inc@localhost:27017/increment');
});
// Add a poll with the above constants
increments.poll(poll, candidates);
/* Start webserver */
var webPort = 8000;
var ioPort = 3000;
var express = require('express');
var app = express();
var async = require('async');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var pug = require('pug');
// Receive votes by accepting a posted message with bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Prevent double submissions by checking cookies with cookieParser
app.use(cookieParser());
// Add template support
app.set('view engine', 'pug');
app.use(express.static('views'))
// Display the voting screen
app.get('/', function(request, responce) {
/* Increments can return an instance code with increments.getInstance
/* Just return the instance code within the POST data of a vote.
/* The easiest way is to use a hidden field with the name='instance' */
increments.getInstance(function (instance) {
// Send the template with the defined candidates & the returned instance key
responce.render('index', {candidates: candidates, instance: instance });
});
});
// GET redirect to root
app.get('/vote', function(request, responce) {
responce.redirect('/');
});
// The POST endpoint to receive a vote
app.post('/vote', function(request, responce) {
/* Increments accepts an object with the following items:
/
/* 'name' The candidate or option you are voting for
/* 'poll' A poll identifier you have already defined
/* 'instance' The variable generated with getInstance
/
/ Voting is accomplished first by creating an object 'ballot' with a name and a poll
/ Pass the instance value if necessary, then execute increments.vote(ballot)
*/
var ballot = {
name: request.body.vote, // name of candidate (input name="vote")
poll: poll, // the name of the poll ('elections')
instance: request.body.instance // hidden input (name="instance")
}
/* Increments accepts the object and sends a responce to Express. */
increments.vote(ballot, function(err, data) {
if (err) throw (err);
responce.redirect('/statistics');
increments.statistics(poll, function (err, results) {
io.sockets.emit('statistics', results);
})
});
});
// Display the statistics page
app.get('/statistics', function( request, responce ) {
/* Increments will return statistics with increments.statistics
/* Data is returned in JSON and rendered to the template.*/
increments.statistics(poll, function (err, results) {
responce.render('index', { statistics: results } );
});
});
// API GET Request
app.get('/statistics/data', function( request, responce ) {
/* Increments can return RESTfully to an API */
increments.statistics(poll, function (err, results) {
if (err) throw (err);
responce.send(results);
});
});
// Start the webserver
var chalk = require('chalk');
app.listen(webPort);
console.log(chalk.green.bold('Server started listening on port: '+webPort))
var io = require('socket.io')(ioPort);
// Send statistical data to IO instantly