This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (51 loc) · 1.67 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
"use strict";
const request = require('request');
const http = require('http');
const url = require('url');
let PORT = process.env.PORT || 8000;
process.argv.forEach(function (arg) {
if(arg.indexOf('--port=') === 0) {
PORT = parseInt(arg.replace('--port=', ''), 10);
}
});
http.createServer((req, response) => {
let remoteURL = url.parse(req.url.substring(1));
if(!remoteURL.hostname || remoteURL.hostname === 'localhost') return response.end();
if(req.method === 'OPTIONS') {
response.writeHead(200, {
'access-control-allow-origin': req.headers.origin || '*',
'access-control-allow-headers': 'Origin, X-Requested-With, Content-Type, Accept, authorization'
});
return response.end();
}
for(let key in req.headers) {
if(key.match(/host|cookie/ig)){
delete req.headers[key];
}
}
let config = {
url: remoteURL,
followAllRedirects: true,
method: req.method,
headers: req.headers,
gzip: true
};
if(req.method !== 'HEAD') {
config.body = req;
}
request(config)
.on('response', res => {
res.headers['access-control-allow-origin'] = req.headers.origin || '*';
res.headers['access-control-allow-headers'] = 'Origin, X-Requested-With, Content-Type, Accept, authorization';
delete res.headers['access-control-allow-credentials'];
delete res.headers['access-control-allow-methods'];
delete res.headers['content-encoding'];
delete res.headers['content-length'];
response.writeHead(res.statusCode, res.headers);
})
.on('data', function(data) {
response.write(data);
})
.on('error', () => response.end())
.on('end', () => response.end());
}).listen(PORT);