This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
56 lines (47 loc) · 1.53 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
const http = require('http')
const express = require('express')
const config = require('./config/config.js')
const logger = require('./lib/logger.js')
const migrator = require('./lib/migrator')
const compression = require('compression')
const WebSocket = require('ws')
const socketController = require('./lib/controllers/websockets.js')
const app = express()
const server = http.createServer(app)
// set headers for every request
app.disable('x-powered-by')
if (config.dist !== false) {
app.use(compression({ threshold: 200 }))
}
app.use(function(req, res, next) {
res.setHeader('X-Frame-Options', 'SAMEORIGIN')
res.setHeader('X-Content-Type-Options', 'nosniff')
res.setHeader('X-XSS-Protection', '1; mode=block')
next()
})
const cb = function(req, res) {
res.sendFile(config.dist + '/index.html')
}
// only start the api once the db is ready
migrator.migrate().then(function() {
if (config.dist !== false) {
app.use('/a', require('./lib/router.js'))
// static index routing
app.use('/', express.static(config.dist))
app.get('/', cb)
app.get('/*', cb)
// if we turn off the dist routing, we're only hosting API
} else {
// but we also want it to work on the ALB.
const router = require('./lib/router.js')
app.use('/a', router)
app.use('/', router)
}
})
const wss = new WebSocket.Server({ server })
wss.on('connection', socketController.connection)
// the router routes stuff through this port
const port = config.port
server.listen(port, () => {
logger.info({ port: port }, 'server listening')
})