-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProxyApp.js
51 lines (51 loc) · 1.25 KB
/
ProxyApp.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
var express = require('express');
var httpProxy = require('http-proxy');
var HttpProxyRules = require('./HttpProxyRules');
function App(site, { proxyOptions, cors }) {
var proxy = httpProxy.createProxyServer(proxyOptions);
function matchHost(req) {
var host = req.header('Host');
var [host, port] = host.split(':');
return site.vhost[host];
}
var app = express();
app.use(cors);
app.use(function (req, res, next) {
var proxyRules = new HttpProxyRules(site.proxyRule);
var target = matchHost(req) || proxyRules.match(req);
if (target) {
if (target.startsWith('app:')) {
var name = target.substr("app:".length);
var a = site.app[name];
if (a && a._port)
target = `http://localhost:${a._port}`;
else {
res.status(404)
.contentType('text/plain')
.send("App not found.");
return;
}
}
proxy.web(req, res, { target: target }, function (e) {
switch (e.code) {
case "ENOTFOUND":
res.status(404);
break;
case "ECONNREFUSED":
res.status(502);
break;
case "ETIMEDOUT":
res.status(504);
break;
default:
res.status(500);
}
res.contentType('text/plain').send(e.message);
});
}
else
next();
});
return app;
}
module.exports = App;