-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
60 lines (54 loc) · 1.94 KB
/
proxy.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
const path = require('path'),
http = require('http'),
httpProxy = require('http-proxy');
const proxy = httpProxy.createProxy();
const proxyOptions = {
'counteroffer.me': 'http://localhost:3001',
'counteroffer.io': 'http://localhost:3002',
// 'counteroffer.app': 'http://localhost:3002' // disable until I get SSL working as .app is SSL-only
};
const parseUrlForDomain = (url) => {
// ~ '/counteroffer.me'
return url.match(/^\/(.*)/)[1];
};
const parseRefererForDomain = (referer) => {
// ~ 'http://localhost/counteroffer.me'
const match = referer.match(/http:\/\/localhost(:\d+)?\/([^\/]+)\/?/);
return match ? match[2] : null;
};
const server = http.createServer((req, res) => {
// if (req.headers.host.indexOf('http://counteroffer.me') > -1) {
// res.writeHead(302, {
// 'Location': 'http://counteroffer.me/bob.stark'
// });
// return res.end();
// }
let proxyTarget;
if (req.headers.host.indexOf('localhost') > -1) {
// console.log('*** in localhost mode');
// development
let domainName = parseUrlForDomain(req.url); // for the initial page load
if (proxyOptions[domainName]) {
// console.log('*** found a domain in URL');
proxyTarget = proxyOptions[domainName];
req.url = '';
} else if (req.headers.referer) {
// console.log('*** found a domain in referer');
domainName = parseRefererForDomain(req.headers.referer); // for fetching resources after initial load
proxyTarget = proxyOptions[domainName];
// console.log('*** proxyTarget: ', proxyTarget);
}
} else {
// production
proxyTarget = proxyOptions[req.headers.host];
}
if (proxyTarget) {
// console.log('*** proxying request: ', req, ' to target: ', proxyTarget);
proxy.web(req, res, { target: proxyTarget });
} else {
res.writeHead(404, res.headers);
}
}).listen(process.env.PORT || 80);
server.on('listening', () => {
console.log('Listening on ', server.address());
});