Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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