mdot_server/app.js
Martin Donnelly de8af028e5 Updated to use PGSql backend
Before convertion of graph object to use AMCharts..
2016-08-11 15:34:52 +01:00

91 lines
2.1 KiB
JavaScript

/**
* Created by Martin on 08/02/2016.
*/
'use strict';
var express = require('express');
var path = require('path');
var http = require('http');
var ejs = require('ejs');
var morgan = require('morgan');
var cookieparser = require('cookie-parser');
var session = require('express-session');
var methodoverride = require('method-override');
var bodyparser = require('body-parser');
var errorhandler = require('errorhandler');
var log4js = require('log4js');
var logger = log4js.getLogger();
var WebSocketServer = require('websocket').server;
var EventEmitter = require('events');
var busEmitter = new EventEmitter();
var apn = require('apn');
require('sugar-date');
var isProduction = false;
var mdotApi = require('./lib/mdot/api.js');
var mdotApiV2 = require('./lib/mdot/apiv2.js');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
if (process.env.NODE_ENV === 'production') {
isProduction = true;
}
logger.warn('isProduction:', isProduction);
var app = express();
app.set('port', process.env.PORT || 4545);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(morgan('combined'));
app.use(cookieparser('your secret here'));
app.use(session({
secret: '1234567890QWERTY', resave: false, saveUninitialized: false
}));
/* 'default', 'short', 'tiny', 'dev' */
app.use(methodoverride());
app.use(bodyparser.urlencoded({extended: false}));
// Parse application/json
app.use(bodyparser.json());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
// Run npm start --production to use dist
var staticDir = isProduction ? 'dist' : 'app';
app.use(express.static(path.join(__dirname, staticDir)));
app.use(errorhandler({dumpExceptions: true, showStack: true}));
// Events and sockets
function originIsAllowed(origin) {
// Put logic here to detect whether the specified origin is allowed.
return true;
}
// glue routes
mdotApi(app);
mdotApiV2(app);
//app.get('/api/mdot/:id', mDot.getData);
app.listen(3010, function() {
logger.info('Express listening on 3010');
});