-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (104 loc) · 3.24 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
126
127
128
129
130
const fs = require('fs')
const http = require('http')
const https = require('https')
const path = require('path')
const express = require('express')
const WebSocket = require('ws')
const mongoose = require('mongoose')
const logger = require('morgan')
const favicon = require('serve-favicon')
const index = require('./routes/index')
const middleware = require('./middleware')
const app = express()
const server = http.createServer(app)
const secureServer = https.createServer({
key: fs.readFileSync('localhost.key'),
cert: fs.readFileSync('localhost.crt'),
})
secureServer.listen(8081, (err) => {
if (err)
console.error(err)
console.log('Secure server listening @', 8081)
})
const wss = new WebSocket.Server({ server: server })
const PORT = process.env.PORT || 8080
const IS_DEV = process.env.NODE_ENV === 'DEV'
server.listen(PORT, () => {
console.log('Server listening @', PORT)
})
wss.on('connection', (ws) => {
ws.on('message', (data) => {
console.log('Socket connected:', data)
ws.poll_id = data
})
})
wss.broadcast = (id, option) => {
const data = option
console.log('Broadcast:', id, '<--', data)
wss.clients.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN && ws.poll_id === id)
ws.send(data)
})
}
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
app.use(logger('dev'))
app.use(favicon(path.join(__dirname, 'public', 'img/favicon.png')))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'public')))
app.use('/', index)
const Poll = require('./models/Poll')
const router = express.Router()
router.get('/:id', (req, res, next) => {
Poll.findById(req.params.id, {}, {_id: false})
.then((data) => res.json(middleware.user_view(data)))
.catch((err) => next(err))
})
router.post('/', async (req, res, next) => {
const data = {
_id: await Poll.countDocuments(),
question: req.body.question,
options: req.body.options.map((x, i) => {
return { id: i, text: x, votes: [] }
})
}
Poll.create(data)
.then((data) => res.json(data._id))
.catch((err) => next(err))
})
router.put('/:id', async (req, res, next) => {
const id = req.params.id
const option = req.body.option
let poll = await Poll.findById(id)
const ip = req.connection.remoteAddress
const has_voted = poll.options.reduce((x, y) => x |= y.votes.includes(ip), false)
if (has_voted) {
console.log('ip already voted:', ip)
return res.sendStatus(204)
}
Poll.findOneAndUpdate({_id: id, 'options.id': option}, {
$addToSet: {'options.$.votes': ip}
})
.then(() => {
wss.broadcast(id, option)
res.sendStatus(200)
})
.catch((err) => next(err))
})
app.use('/polls', router)
app.use((err, req, res) => {
res.locals.message = err.message
res.locals.error = IS_DEV ? err : {}
app.use('/', index)
res.status(err.status || 500)
res.render('error')
})
mongoose.Promise = global.Promise
mongoose.connect(IS_DEV ? 'mongodb://localhost/strawpoll' : 'mongodb://mango:yPThEQLWVkIFd53T6FYfQq05jFK1W1P39sQXxt89p2891FGUGjJhtbwk1EeVFoq1NcZ2qJw92V0AQdNZ5dVLZw==@mango.documents.azure.com:10255/strawpoll?ssl=true&replicaSet=globaldb', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => console.log('MongoDB Connected'))
.catch(console.error)