-
-
Save anonymous/e6def9d062ad737f5479 to your computer and use it in GitHub Desktop.
Gist created using https://github.com/niftylettuce/gedit-gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # Bouncy | |
var bouncy = require('bouncy') | |
, fs = require('fs') | |
, path = require('path') | |
, servers = {} | |
, json = path.join(__dirname, 'servers.json') | |
, reload = function(req, bounce) { | |
return fs.readFile(json, 'utf8', function(err, data) { | |
if (err) { | |
console.log(err); | |
} else { | |
servers = JSON.parse(data); | |
console.log(servers); | |
if (req.headers.host in servers) { | |
bounce(servers[req.headers.host]); | |
} else { | |
bounce(8081); | |
} | |
} | |
}); | |
}; | |
bouncy(function(req, bounce) { | |
reload(req, bounce); | |
}).listen(8000); | |
fs.watchFile(json, function(curr, prev) { | |
console.log("Changes detected in servers.json"); | |
if (curr.mtime.getTime() !== prev.mtime.getTime()) { | |
bouncy(function(req, bounce) { | |
console.log("Host: ", req.headers.host); | |
reload(req, bounce); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment