This repository was archived by the owner on Mar 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (109 loc) · 3.22 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
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
import express from 'express'
import cookieParser from 'cookie-parser'
import bodyParser from 'body-parser'
import { CLIENT_ID, CLIENT_SECRET, SESSION_SECRET } from './config.js'
import models from './models'
import moment from 'moment'
import setUserInfo from './lib/setUserInfo'
import refreshCurrentElectionInfo from './lib/refreshCurrentElectionInfo'
const elections = require('./routes/elections')
const candidates = require('./routes/candidates')
const positions = require('./routes/positions')
const users = require('./routes/users')
const userGroups = require('./routes/userGroups')
const votes = require('./routes/votes')
const login = require('./routes/login')
const ballot = require('./routes/ballot')
const app = express()
let currentElectionInfo = {
id: -1,
checked: moment(),
isStale: true
}
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
// Add headers
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
)
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type,Authorization'
)
res.setHeader('Access-Control-Allow-Credentials', true)
next()
})
// election middleware
app.use(async (req, res, next) => {
if (req.body.electionId) {
req.electionId = req.body.electionId
console.log('already has id in body')
next() // there is a electionId in the body already, we don't need to add one
return
}
if (req.query.electionId) {
req.electionId = req.query.electionId
console.log('already has id in query')
next()
return
}
if (req.params.electionId) {
req.electionId = req.params.electionId
console.log('already has id in params')
next()
return
}
currentElectionInfo = await refreshCurrentElectionInfo(currentElectionInfo)
console.log(currentElectionInfo)
req.electionId = currentElectionInfo.id
req.election = currentElectionInfo
next()
return
})
// authentication middleware
app.use(async (req, res, next) => {
// if the user is trying to log in, then we don't care what their status is
if (req.originalUrl.includes('login')) {
next()
}
// if the user's authentication header is set, we can get their information and add it to the req object
req = await setUserInfo(req)
next()
})
// Plural routes
app.use('/elections', elections)
app.use('/candidates', candidates)
app.use('/positions', positions)
app.use('/users', users)
app.use('/userGroups', userGroups)
app.use('/votes', votes)
app.use('/ballots', ballot)
// Singular routes
app.use('/election', elections)
app.use('/candidate', candidates)
app.use('/position', positions)
app.use('/user', users)
app.use('/userGroup', userGroups)
app.use('/vote', votes)
app.use('/ballot', ballot)
app.use('/login', login)
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found')
err.status = 404
next(err)
})
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// render the error page
res.status(err.status || 500)
})
module.exports = app
app.listen(4000)