Aida-Server/server.js
2017-11-09 00:05:30 +00:00

85 lines
2.3 KiB
JavaScript

/**
* Created by mdonnel on 07/06/2017.
*/
const express = require('express');
const path = require('path');
const http = require('http');
const morgan = require('morgan');
const cookieParser = require('cookieparser');
const session = require('express-session');
const methodoverride = require('method-override');
const bodyparser = require('body-parser');
const errorhandler = require('errorhandler');
const logger = require('log4js').getLogger();
const SocketHandler = require('./lib/wshandlerv2');
const Aida = require('./lib/aida');
const EventEmitter = require('events').EventEmitter;
const WebSocket = require('ws');
const app = express();
GLOBAL.lastcheck = 0;
const port = process.env.PORT || 9010;
const isProduction = (process.env.NODE_ENV === 'production');
const staticDir = isProduction ? 'dist' : 'app';
logger.warn('isProduction:', isProduction);
// app.configure(function () {
app.set('port', port);
app.set('view engine', 'ejs');
app.use(morgan('dev'));
// app.use(cookieParser('your secret here'));
/* app.use(session({
secret: 'd2jRT6ZpYFsXsF3kGS21ZszKbPAaEa', 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.all('/*', (req, res, next) => {
// CORS headers
res.header('Access-Control-Allow-Origin', '*'); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method === 'OPTIONS')
res.status(200).end();
else
next();
});
// app.use(app.router);
app.use(express.static(path.join(__dirname, staticDir)));
app.use(errorhandler({ 'dumpExceptions': true, 'showStack': true }));
/*app.get('/', function(req, res) {
// res.render('pages/slackV2-min');
res.render('pages/index');
});*/
const aidaServer = new Aida();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const webSocket = new SocketHandler(EventEmitter, wss);
// server.on('request', app);
server.listen(port, () => {
logger.info(`New server listening on ${ server.address().port}`);
});