-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathproxy.js
40 lines (36 loc) · 1.05 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
var util = require("util"),
http = require("http"),
url = require("url");
var port = parseInt(process.argv[2]) || 8081;
var options = function(request) {
var uri = url.parse(request.url);
var opt = {
host: uri.hostname,
port: uri.port || 80,
path: uri.pathname,
method: request.method,
headers: request.headers
};
opt.headers['connection'] = 'keep-alive';
return opt;
}
http.createServer(function(request, response) {
util.log("--> " + request.url);
var remoteRequest = http.request(options(request), function (remoteResponse) {
response.writeHead(remoteResponse.statusCode, remoteResponse.headers);
remoteResponse.on('data', function (chunk) {
response.write(chunk);
});
remoteResponse.on('end', function () {
util.log("<-- " + response.statusCode + " " + request.url);
response.end();
});
});
request.on('data', function (chunk) {
remoteRequest.write(chunk);
});
request.on('end', function () {
remoteRequest.end();
});
}).listen(port);
util.log('Listening on port ' + port);