Skip to content

Instantly share code, notes, and snippets.

@tsing
Forked from pgr0ss/proxy_with_balance_file.js
Created February 11, 2010 16:36
Show Gist options
  • Save tsing/301678 to your computer and use it in GitHub Desktop.
Save tsing/301678 to your computer and use it in GitHub Desktop.
var posix = require('posix'),
sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
checkBalanceFile(req, res);
}).listen(8000);
function checkBalanceFile(req, res) {
var promise = posix.stat("balance");
promise.addCallback(function () {
passThroughOriginalRequest(req, res);
});
promise.addErrback(function () {
setTimeout(function() {checkBalanceFile(req, res)}, 1000);
});
}
function passThroughOriginalRequest(req, res) {
var request = http.createClient(2000, "localhost").request("GET", req.url, {});
request.finish(function (response) {
res.sendHeader(response.statusCode, response.headers);
response.addListener("body", function (chunk) {
res.sendBody(chunk);
});
response.addListener("complete", function () {
res.finish();
});
});
}
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