-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (52 loc) · 1.94 KB
/
index.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
var express = require('express');
var Profile = require('fair-vote-profile');
var bodyParser = require('body-parser');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var configDB = require('./config/database.js');
var elections = {};
require('./config/passport')(passport);
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({ secret: 'theresanalieninmycloset' }));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect(configDB.url);
app.post('/election/:election',function (req, res){
var electionName = req.params.election;
console.log(electionName);
elections[electionName] = new Profile();
res.send("did it :)");
});
app.post('/election/:election/vote',passport.authenticate('vote',{session: false}),function (req, res) {
var electionName = req.params.election;
var ballot = req.body.vote;
console.log(ballot);
if(!elections[electionName]){
elections[electionName] = new Profile("pairwise");
elections[electionName].setAlternatives(ballot);
}
elections[electionName].vote(ballot);
res.send("did it : )");
});
app.post('/signup', passport.authenticate('addUser',{session: false}),function (req,res){
res.send("hey");
});
app.get('/election/:election/score/:rule',function(req,res){
var electionName = req.params.election;
res.send(elections[electionName].score(req.params.rule));
});
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});