forked from desaintmartin/proxy-by-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (108 loc) · 3.69 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
107
108
109
110
111
112
113
114
115
//
// This is an example of a url-routing middleware.
// This is not intended for production use, but rather as
// an example of how to write a middleware.
//
var fs = require('fs');
function getMatchers(urls) {
var matchers = [],
matcher,
url,
r;
for (url in urls) {
// Call the 'matcher' function above, and store the resulting closure.
// First, turn the URL into a regex.
// NOTE: Turning user input directly into a Regular Expression is NOT SAFE.
r = new RegExp(url.replace(/\//, '^\\/'));
// This next block of code may look a little confusing.
// It returns a closure (anonymous function) for each URL to be matched,
// storing them in an array - on each request, if the URL matches one that has
// a function stored for it, the function will be called.
matcher = (function(r) {
var dest = urls[url];
return function (url) {
var m = url.match(r);
if (!m) {
return;
}
var path = url.slice(m[0].length + 1);
console.log('proxy:', url, '->', dest);
return ({url: path, dest: dest});
}})(r);
matchers.push(matcher);
}
return matchers;
}
module.exports = function (urls) {
var matchers,
urlFile;
if (typeof urls === 'string') {
//
// If we are passed a string then assume it is a
// file path, parse that file and watch it for changes
//
urlsFile = urls,
urls = JSON.parse(fs.readFileSync(urlsFile));
fs.watchFile(urlsFile, function () {
console.log("Reloading urls...");
fs.readFile(urlsFile, function (err, data) {
if (!err) {
try {
urls = JSON.parse(data);
matchers = getMatchers(urls);
}
catch (e) {
console.log("Syntax error in json file.");
}
}
});
});
}
matchers = getMatchers(urls);
// This closure is returned as the request handler.
middleware = function (req, res, next) {
//
// in node-http-proxy middlewares, `proxy` is the prototype of `next`
// (this means node-http-proxy middlewares support both the connect API (req, res, next)
// and the node-http-proxy API (req, res, proxy)
//
var proxy = next;
for (var k in matchers) {
// for each URL matcher, try the request's URL.
var m = matchers[k](req.url);
// If it's a match:
if (m) {
// Replace the local URL with the destination URL.
req.url = m.url;
// If routing to a server on another domain, the hostname in the request must be changed.
req.headers.host = m.host;
// Once any changes are taken care of, this line makes the magic happen.
return proxy.proxyRequest(req, res, m.dest);
}
}
next() //if there wasno matching rule, fall back to next middleware.
}
// this closure is attached to the request handler for websocket proxying
middleware.proxyWebSocketRequest = function (req, socket, head, next) {
//
// Same as above, but for WebSocket
//
console.log("Websocket proxying : " + req.url);
var proxy = next;
for (var k in matchers) {
// for each URL matcher, try the request's URL.
var m = matchers[k](req.url);
// If it's a match:
if (m) {
// Replace the local URL with the destination URL.
req.url = m.url;
// If routing to a server on another domain, the hostname in the request must be changed.
req.headers.host = m.host;
// Once any changes are taken care of, this line makes the magic happen.
return proxy.proxyWebSocketRequest(req, socket, head, m.dest);
}
}
next() //if there wasno matching rule, fall back to next middleware.
}
return middleware;
}