-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (59 loc) · 1.61 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
var express = require('express')
, http = require('http')
, path = require('path')
, httpProxy = require('http-proxy');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
//Initialize system variables
var config = require('./config');
/**
* Configure app
*/
var app = module.exports = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/client/js/core');
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'client')));
/**
* Catch if user wants to go to websocket
*/
var proxy = httpProxy.createProxy({target:'http://localhost:5500/socket.io/', agent: new http.Agent({maxSockets:15})});
app.use(function (req, res, next) {
if (req.url.indexOf('/socket.io/')>-1) {
proxy.web(req, res, function(err) {
console.error('http-proxy-websocket message:' + err);
res.send(err, 500);
});
//
} else {
next();
}
});
/**
* DEFINE ROUTES
*/
var routes = require('./server/routes/angularRoutes');
//Setup routes
require('./server/routes/setupRouting.js')(app, routes);
/**
* Configure Server
*/
app.set('port', process.env.PORT || config.port);
var server = http.createServer(app);
/**
* Watch for websocket upgrades
*/
server.on('upgrade',function(req,socket, head){
proxy.ws(req, socket, head, function(err) {
console.log('upgrade', err);
socket.write('disconnect');
});
});
/**
* Start server
*/
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});