-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
46 lines (36 loc) · 1.14 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
var defaults = {
"host": "localhost",
"port": "3000",
"targethost": "localhost",
"targetport": "80",
};
var http = require("http");
var config = require("optimist")
.default(defaults)
.argv;
http.createServer(function(request, response) {
console.log("Processing", request.method, request.url);
var targetRequest = http.request({
host: config.targethost,
port: config.targetport,
path: request.url,
method: request.method,
headers: request.headers
}, function(targetResponse) {
response.writeHead(targetResponse.statusCode, targetResponse.headers);
targetResponse.on("data", function(chunk) {
response.write(chunk);
});
targetResponse.on("end", function() {
response.end();
});
});
request.on("data", function(chunk) {
targetRequest.write(chunk);
});
request.on("end", function() {
targetRequest.end();
});
}).listen(config.port, config.host);
console.log("Listening on " + config.host + ":" + config.port);
console.log("Forwarding to " + config.targethost + ":" + config.targetport);