-
Notifications
You must be signed in to change notification settings - Fork 12
/
routeProxy.js
33 lines (29 loc) · 952 Bytes
/
routeProxy.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
var http = require('http'),
httpProxy = require('http-proxy'),
HttpProxyRules = require('http-proxy-rules');
var config = require("./config.json");
var rules={};
try{
rules=require('./route-proxy.json');
} catch(err){
}
var proxyRules = new HttpProxyRules({
rules:rules
});
console.log(proxyRules);
// Create reverse proxy instance
var proxy = httpProxy.createProxy();
// Create http server that leverages reverse proxy instance
// and proxy rules to proxy requests to different targets
http.createServer(function(req, res) {
// a match method is exposed on the proxy rules instance
// to test a request to see if it matches against one of the specified rules
var target = proxyRules.match(req);
if (target) {
return proxy.web(req, res, {
target: target
});
}
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 not found');
}).listen(config.internalProxyPort);