-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
50 lines (40 loc) · 1.46 KB
/
server.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
require('./functions/tmp-precheck')()
const express = require('express')
const fs = require('fs')
const mainRouter = require('./router/index.js')
const keepAlive = require('./router/keepAlive.js')
const apiV1 = require('./router/v1-api')
const { fetchAllGTFS, startAllRefreshVP } = require('./lib/loopers')
const app = express()
process.env.VP_UPDATE_COUNT = 0
const CronJob = require('cron').CronJob
const feedUpdater = new CronJob('0 0 */1 * * *', fetchAllGTFS) // every hour at the 00:00 mark
async function start () {
const host = process.env.$HOST || process.env.HOST || '127.0.0.1'
const port = process.env.$PORT || process.env.PORT || 3000
// start cron jobs
feedUpdater.start()
app.disable('x-powered-by')
app.use('/api/v1', apiV1)
app.use('/realtime', keepAlive)
app.use(express.static('./static', { dotfiles: 'ignore' }))
app.use(express.static('./tmp'))
app.use(mainRouter)
app.use((req, res, next) => {
var err = new Error('Not Found')
err.status = 404
next(err)
})
app.use((err, req, res, next) => {
const status = err.status || 500
const template = fs.readFileSync('./static/.err.html', 'utf8')
const intpl = template.replace(/(\$ERROR_CODE)/gm, status)
res.setHeader('Content-Type', 'text/html')
res.status(status).send(intpl)
})
// Listen the server
app.listen(port, () => console.log(`Server listening on http://${host}:${port}`))
await fetchAllGTFS() // download gtfs
startAllRefreshVP()
}
start()