const http = require('http'); const express = require('express'); const cors = require('cors'); const path = require('path'); const apicache = require('apicache'); const logger = require('log4js').getLogger('Server'); const train = require('./server/lib/train'); const twitter = require('./server/lib/twitter'); const WebSocket = require('ws'); const SocketHandler = require('./server/lib/wshandlerv3'); const Events = require('events'); const busEmitter = new Events.EventEmitter(); logger.level = process.env.LOGGER_LEVEL || 'debug'; const app = express(); const port = process.env.PORT || 8100; const sitePath = 'live'; apicache.options({ 'debug': true }); const cache = apicache.middleware; app.use(express.json()); app.use(express.static(path.join(__dirname, sitePath))); app.options('*', cors()); // include before other routes app.use('/gettrains', cors(), cache('60 seconds'), train.getTrainTimes); app.use('/getnexttraintimes', cors(), cache('60 seconds'), train.getNextTrainTimes); app.use('/getroute', cors(), cache('60 seconds'), train.getRoute); app.use('/getnews', cors(), cache('30 minutes'), train.getNews); app.use('/getservice', cors(), cache('60 seconds'), train.getService); app.use('/twitter', cors(), twitter.all); process.on('uncaughtException', err => { logger.error(err); }); /* app.listen(port, (err) => { if (err) return logger.error('Server error:', err); logger.info(`TrainTime Server is listening on ${port}`); });*/ const server = http.createServer(app); try { const wss = new WebSocket.Server({ server }); const webSocket = new SocketHandler(busEmitter, wss); twitter.updates(busEmitter); } catch (e) { logger.error(e); } server.listen(port, (err) => { if (err) return logger.error('Server error:', err); logger.info(`New Traintime server listening on ${ server.address().port}`); });