forked from Irrelon/node-irrelon-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
92 lines (81 loc) · 2.74 KB
/
router.js
File metadata and controls
92 lines (81 loc) · 2.74 KB
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
///////////////////////////////////////////
// Set the port you want to listen on here
///////////////////////////////////////////
var serverPort = 80;
////////////////////////////////////////////////////////////////////////////
// Don't modify anything below here unless you know what you are doing :) //
////////////////////////////////////////////////////////////////////////////
var configFilePath = __dirname + '/config.js';
var http = require('http'), httpProxy = require('http-proxy'), fs = require('fs');
var proxy = httpProxy.createProxyServer({});
var routerTable = {}
var loadConfigData = function (callback) {
var self = this;
fs.readFile(configFilePath, 'utf8', function (err, data) {
if (err) { console.log('Error reading router config file: ' + err); } else {
eval(data);
console.log('Router table config data updated successfully.');
}
if (typeof callback == 'function') { callback(); }
});
}
var configFileEvent = function (curr, prev) {
if (curr.mtime != prev.mtime) {
// The file has been modified so update the router table
console.log('Router table config data has changed, updating...');
loadConfigData();
}
}
var do404 = function (res) {
res.writeHead(404);
res.write('Nothing to serve from here. Sorry! (Error 404)');
res.end();
}
var server = http.createServer(function (req, res) {
// Check for an entry in the router table
if (routerTable[req.headers.host] != null) {
var route = routerTable[req.headers.host];
if (route.target) {
proxy.web(req, res, route);
} else {
console.log('Cannot route ' + req.headers.host + ' because config entry is missing "target" property.');
do404(res);
}
} else {
do404(res);
}
});
server.on('upgrade', function(req, socket, head) {
// Check for an entry in the router table
if (routerTable[req.headers.host] != null) {
var route = routerTable[req.headers.host];
if (route.target) {
proxy.ws(req, socket, head, route);
} else {
console.log('Cannot route ' + req.headers.host + ' because config entry is missing "target" property.');
}
} else {
console.log('Cannot upgrade socket for websockets because the header host does not exist in the routing table!', req.headers);
}
});
proxy.on('proxyError', function (err, req, res) {
if (routerTable[req.headers.host] != null) {
var route = routerTable[req.headers.host];
if (route.errorRedirect) {
res.writeHead(302, {'Location': route.errorRedirect});
res.end();
} else {
do404(res);
}
} else {
do404(res);
}
return true;
});
// Load the initial config data
loadConfigData(function () {
// Config data was loaded so... start the server
server.listen(serverPort);
// Watch the config file for changes
fs.watchFile(configFilePath, function (curr, prev) { configFileEvent(curr, prev); });
});