-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (42 loc) · 1.14 KB
/
index.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
/* eslint-disable no-console */
const bodyParser = require('body-parser')
const cors = require('cors')
const express = require('express')
const fs = require('fs')
const helmet = require('helmet')
const http = require('http')
const https = require('https')
const morgan = require('morgan')
const rfs = require('rotating-file-stream')
const config = require('./config.json')
const {
port,
logDir,
privateKey,
certificate
} = config
const accessLog = rfs('access.log', {
interval: '1d',
path: logDir || `${__dirname}/logs/`
})
console.log(`Starting in ${process.env.NODE_ENV} mode.`)
const app = express()
app.use(helmet())
app.use(cors())
app.use(bodyParser.json())
app.use(morgan('combined', { stream: accessLog }))
require('./routes')(app)
app.get('/status', (req, res) => {
res.send(200)
})
if (process.env.NODE_ENV === 'production') {
const options = {
cert: fs.readFileSync(certificate),
key: fs.readFileSync(privateKey)
}
https.createServer(options, app).listen(port)
console.log(`HTTPS server started on port ${port}.`)
} else {
http.createServer(app).listen(port)
console.log(`HTTP server started on port ${port}.`)
}