forked from jeffbski/jsconf-react-win
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bs-config.js
131 lines (121 loc) · 2.95 KB
/
bs-config.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
'use strict';
var anyBody = require('body/any');
var Immutable = require('immutable');
var shortid = require('shortid');
var pollResults = Immutable.fromJS({});
var polls = Immutable.List();
var PollRecord = Immutable.Record({
id: null,
question: '',
choices: []
});
polls = polls.push(new PollRecord({
"id": "p1",
"question": "What is 1 + 1?",
"choices": [
"one",
"two"
]
}));
polls = polls.push(new PollRecord({
"id": "p2",
"question": "What is 2 + 2?",
"choices": [
"three",
"four"
]
}));
function getPolls(req, res, next) {
if (req.method === 'GET' && req.url === '/polls') {
var retObj = {
polls: polls.toJS()
};
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify(retObj));
}
next();
}
function addPoll(req, res, next) {
if (req.method === 'POST' && req.url === '/polls') {
return anyBody(req, res, function (err, body) {
if (err) {
console.error(err);
res.statusCode = 500;
return res.end('err: ' + err.toString());
}
body.id = shortid.generate();
polls = polls.push(Immutable.fromJS(body));
var retObj = {
added: {
id: body.id
}
};
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify(retObj));
});
}
next();
}
function vote(req, res, next) {
var url = req.url;
var m = /^\/polls\/(.+)$/.exec(url);
if (m) {
return anyBody(req, res, function (err, body) {
if (err) {
console.error(err);
res.statusCode = 500;
return res.end('err: ' + err.toString());
}
var id = m[1];
var choiceIdx = body[id];
// store vote
pollResults = pollResults.update(id, Immutable.List(), function (pollResult) {
return pollResult.update(choiceIdx, 0, function (prev) {
return (prev || 0) + 1;
});
});
// respond with aggregate
var retObj = {
votes: pollResults.get(id)
};
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify(retObj));
});
}
next();
}
function results(req, res, next) {
if (req.url === '/admin/polls') {
var pollsAndResults = polls.reduce(function (accum, poll) {
poll = poll.toJS();
poll.votes = pollResults.get(poll.id, []);
accum.push(poll);
return accum;
}, []);
var retObj = {
polls: pollsAndResults
};
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify(retObj));
}
next();
}
module.exports = {
browser: ['google chrome'],
// browser: ['google chrome canary'],
port: 3005,
// proxy: 'localhost:3000',
// reloadDelay: 1000,
files: [
'public/*.html',
'public/*.json',
'dist/bundle.js',
'dist/site.min.css'],
server: {
baseDir: './public',
routes: {
'/dist': './dist'
},
middleware: [getPolls, addPoll, vote, results]
}
};