traintimesPWA/server.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-12-15 18:34:59 +00:00
const express = require('express');
const cors = require('cors');
2017-12-15 18:34:59 +00:00
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');
2017-12-15 18:34:59 +00:00
logger.level = 'debug';
const app = express();
2017-12-15 19:12:48 +00:00
const port = process.env.PORT || 8100;
2017-12-15 18:34:59 +00:00
const sitePath = 'live';
apicache.options({ 'debug': true });
const cache = apicache.middleware;
app.use(express.json());
2017-12-15 18:34:59 +00:00
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);
2017-12-15 18:34:59 +00:00
process.on('uncaughtException', err => {
logger.error(err);
});
2017-12-15 18:34:59 +00:00
app.listen(port, (err) => {
if (err)
return logger.error('Server error:', err);
logger.info(`TrainTime Server is listening on ${port}`);
});