-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
106 lines (75 loc) · 2.73 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
var debug = require( 'debug' )( 'spb:server' );
var http = require( 'http' );
var httpProxy = require( 'http-proxy' );
var express = require( 'express' );
var morgan = require( 'morgan' );
var SocketIO = require( 'socket.io' );
// ==================== OPTIONS ==================== //
var HOST = 'localhost';
var API_PORT = process.env.API_PORT || 4001;
var PROXY_PORT = process.env.PROXY_PORT || 4000;
var SOCKET_PATH = 'ws';
// ==================== API SERVER ==================== //
var apiApp = express();
var apiServer = new http.createServer( apiApp );
var io = new SocketIO( apiServer, { path: `/${SOCKET_PATH}` } );
debug( `Socket.io path: ${io._path}` );
io.on( 'connection', function ( socket ) {
debug( 'New socket connection (%s)', socket.conn.transport.name );
socket.emit( 'msg', 'Socket connected OK' );
// emit test messages
var timer = setInterval(function ( ) {
socket.emit( 'msg', 'ping' );
}, 2500);
socket.on( 'disconnect', function ( ) {
debug( 'Socket disconnected' );
clearInterval( timer );
});
});
apiApp.use( morgan( 'dev' ) );
apiApp.get( '/', function ( req, res ) {
res.send( 'Hello from the API server' );
});
apiServer.listen( API_PORT, function ( ) {
debug( `API server listening on http://${HOST}:${API_PORT}` );
});
// ==================== PROXY SERVER ==================== //
var proxyApp = express();
var proxyServer = http.createServer( proxyApp );
var proxy = httpProxy.createProxyServer({
target : `http://${HOST}:${API_PORT}`,
// ws : true,
});
proxyApp.use( morgan( 'dev' ) );
proxy.on( 'error', function ( err ) {
// console.error( err.stack );
debug( 'PROXY ERROR', err );
});
proxy.on( 'proxyReq', function ( proxyReq, req, res ) {
debug( 'Proxy Request', proxyReq.path );
});
proxy.on( 'proxyReqWs', function ( proxyReqWs, req, res ) {
debug( 'Proxy *WS* Request', proxyReqWs.path );
});
// serve the test page
proxyApp.get( '/', function ( req, res ) {
res.sendFile( './index.html', { root: process.cwd() } );
});
// proxy non-socket requests
// * not required to proxy the socket.io connection *
// proxyApp.use( '/api', function ( req, res ) {
// proxy.web( req, res, { target: `http://${HOST}:${API_PORT}` } );
// });
// proxy the socket.io polling requests
proxyApp.use( `/${SOCKET_PATH}`, function ( req, res ) {
proxy.web( req, res, { target: `http://${HOST}:${API_PORT}/${SOCKET_PATH}` } );
});
// proxy the socket.io WS requests
proxyServer.on( 'upgrade', function( req, socket, head ) {
debug( '⚡️ ---------- SOCKET CONNECTION UPGRADING ---------- ⚡️ ' );
proxy.ws( req, socket, head );
});
proxyServer.listen( PROXY_PORT, function ( ) {
debug( `Proxy server listening on http://${HOST}:${PROXY_PORT}`, '\n' );
});