-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathproxy.js
More file actions
65 lines (56 loc) · 2.19 KB
/
Copy pathproxy.js
File metadata and controls
65 lines (56 loc) · 2.19 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
const httpProxy = require("http-proxy");
var proxyServer = null;
/**
* Simple Http Proxy, proxies request where url is contained in config to target from config
*
* @param req
* @param res
* @param {object} configs - proxyConfig from config file
* @return {boolean} - will return whether request is being proxied
*/
function proxyHandler(req, res, configs) {
if (!configs || configs.length === 0) {
throw new Error(
"no proxy configs present, please configure in your config file"
);
}
let proxyHit = false;
configs.forEach((config) => {
if (!proxyHit) {
if (!config.forward || config.forward.length === 0) {
throw new Error(
"forward is missing or empty in your proxyConfig"
);
}
if (!config.target || config.target === "") {
throw new Error(
"target is missing or empty in your proxyConfig"
);
}
proxyHit = config.forward.some(
(url) => req.url.indexOf(url) !== -1
);
if (proxyHit) {
// Clone the proxy config in order to make changes without affecting the original config object
const proxyConfig = { ...config };
const protocol = config.protocol ? config.protocol : "https";
proxyConfig.target = `${protocol}://${proxyConfig.target}`;
console.log(
`Proxying request ${req.method}, ${req.url} to ${proxyConfig.target}`
);
// Remove "forward" property since we've already handled the forwarding logic
delete proxyConfig["forward"];
if (proxyServer === null) {
proxyServer = httpProxy.createProxyServer(proxyConfig);
}
proxyServer.web(req, res, (e, req, res) => {
console.error(`Proxy error: ${e.code}`);
res.writeHead(502);
res.end();
});
}
}
});
return proxyHit;
}
module.exports = proxyHandler;