-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathproxy.js
More file actions
33 lines (30 loc) · 830 Bytes
/
proxy.js
File metadata and controls
33 lines (30 loc) · 830 Bytes
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
var http = require('http'),
url = require('url');
var ProxyServer = {
onRequest: function(client_req, client_res, host, callback) {
var proxyUrl = url.parse(host);
var options = {
path: client_req.url,
hostname: proxyUrl.hostname,
port: proxyUrl.port,
method: client_req.method,
headers: client_req.headers
};
var proxy = http.request(options, function (res) {
var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
//Replace
callback(res, Buffer.concat(chunks));
});
}).on('error', function(e) {
client_res.writeHead(500);
client_res.write('error: ' + e.toString());
client_res.end();
});
proxy.end();
}
};
exports.proxyServer = ProxyServer;