-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.js
66 lines (55 loc) · 1.86 KB
/
rest.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
// Require relative to project root
require('rootpath')()
const config = require('config')
const { logger } = require('utils/logger')
// Initialize logger
logger.setLevel(config.LOG_LEVEL)
logger.info('Starting server...')
const app = require('express')()
const https = require('https')
// TODO: require('utils/rate-limit')
// Register request middleware (order matters)
app.use(require('helmet')())
app.use(require('cors')({
origin: config.CORS_DOMAINS,
methods: 'GET,POST,PUT,DELETE',
credentials: true
}))
app.use(logger.requestLogger)
app.use(require('cookie-parser')())
app.use(require('utils/jwt').verificationHandler)
app.use(require('utils/jwt').refreshHandler)
app.use(require('body-parser').urlencoded({ extended: false }))
app.use(require('body-parser').json())
app.use(require('utils/account-logger').accountLogger)
// Register API Routes
app.use(config.API_PREFIX + '/users', require('routes/users'))
app.use(config.API_PREFIX + '/auth', require('routes/auth'))
// Register error handling middleware
app.use(require('utils/errors').handleError)
// Start server
let server
if (config.HTTPS_ENABLED) {
// HTTPS server
const fs = require('fs')
server = https.createServer({
key: fs.readFileSync(config.HTTPS_KEY_FILE),
cert: fs.readFileSync(config.HTTPS_CERT_FILE)
}, app).listen(config.PORT)
} else {
// HTTP server
server = app.listen(config.PORT, () => logger.info('Server listening on port: ' + config.PORT))
}
const db = require('utils/db')()
// Closing server when process is stopped
process.on('SIGINT', () => {
// Closing MongoDB connection
logger.info('Closing MongoDB connection...')
db.close(false, () => logger.info('MongoDB connection closed'))
// Closing HTTP server
logger.info('Closing server...')
server.close(() => {
logger.info('Server closed')
process.exit(0)
})
})