-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
71 lines (50 loc) · 1.76 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
var http = require('http');
var https = require('https');
var join = require('join-path');
var httpProxy = require('http-proxy');
var url = require('fast-url-parser');
var PROXY = httpProxy.createProxyServer({
changeOrigin: true
});
var DEFAULT_TIMEOUT = 30000;
module.exports = function () {
return function (req, res, next) {
if (!req.service || !req.service.config) return next();
var requestUrlValues = (req.service.path || req.url).split('/');
var proxyName = requestUrlValues[2];
var config = getEndpointConfig(proxyName);
if (!config || !config.origin) return next();
var endpointUri = requestUrlValues.slice(3).join('/');
// Set headers
Object.keys(config.headers || {}).forEach(function (key) {
var val = config.headers[key];
req.headers[key.toLowerCase()] =
(req.headers[key.toLowerCase()] || config.headers[key]);
});
if (config.cookies === false) delete req.headers.cookie;
// Set relative path
req.url = join('/', endpointUri);
PROXY.web(req, res, proxyConfig());
function getEndpointConfig (name) {
return req.service.config[name] || req.service.config[name.toLowerCase()]
}
function proxyConfig () {
// Set up proxy agent
var proxyAgent = http;
var _proxyConfig = {
target: config.origin
// timeout: config.timeout || DEFAULT_TIMEOUT
};
try {
var parsed = url.parse(config.origin);
agent = (parsed._protocol === 'https') ? https : http;
_proxyConfig.headers = {
host: parsed.host
};
}
catch (e) {}
_proxyConfig.agent = agent.globalAgent;
return _proxyConfig;
}
};
};