-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
60 lines (51 loc) · 1.25 KB
/
server.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
const path = require('path')
const cors = require('cors')
const express = require('express')
const moment = require('moment')
const pollerService = require('./poller')
const app = express()
const port = process.argv.PORT || 9000
const stages = {
finished: [4],
active: [1, 2, 3],
proposed: [0]
}
app.disable('x-powered-by')
app.set('json spaces', 2)
app.set('view engine', 'pug')
app.use(cors())
app.use(express.static(path.join(__dirname, 'public')))
app.get('/:type(finished|active|proposed)?', (req, res, next) => {
const { type } = req.params
const proposals = filterProposals(type)
if (!proposals) {
next('route')
return
}
res.render('index.pug', { proposals, moment })
})
app.get('/proposals/:type(finished|active|proposed)?', (req, res, next) => {
const { type } = req.params
const proposals = filterProposals(type)
if (!proposals) {
next('route')
return
}
res.json(proposals)
})
app.listen(port, () =>
console.log(`app listening on port ${ port }`)
)
pollerService.poll()
function filterProposals(type) {
const proposals = pollerService.get()
if (!proposals) {
return null
}
if (type) {
return proposals.filter(proposal =>
stages[type].includes(proposal.stage)
)
}
return proposals
}