Skip to content

Instantly share code, notes, and snippets.

@kyungw00k
Forked from pgr0ss/proxy_with_balance_file.js
Created November 23, 2010 17:34
Show Gist options
  • Save kyungw00k/712157 to your computer and use it in GitHub Desktop.
Save kyungw00k/712157 to your computer and use it in GitHub Desktop.
var fs = require('fs'),
sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
checkBalanceFile(req, res);
}).listen(8000);
function checkBalanceFile(req, res) {
fs.stat("balance", function(err) {
if (err) {
setTimeout(function() {checkBalanceFile(req, res)}, 1000);
} else {
passThroughOriginalRequest(req, res);
}
});
}
function passThroughOriginalRequest(req, res) {
var request = http.createClient(2000, "localhost").request("GET", req.url, {});
request.addListener("response", function (response) {
res.writeHeader(response.statusCode, response.headers);
response.addListener("data", function (chunk) {
res.write(chunk);
});
response.addListener("end", function () {
res.close();
});
});
request.close();
}
sys.puts('Server running at http://127.0.0.1:8000/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment