Skip to content

Instantly share code, notes, and snippets.

@lstoll
Created February 23, 2010 03:09
Show Gist options
  • Save lstoll/311813 to your computer and use it in GitHub Desktop.
Save lstoll/311813 to your computer and use it in GitHub Desktop.
var sys = require('sys')
var http = require('http');
var memcache_lib = require('./memcache');
var memcache = new memcache_lib.Client();//"127.0.0.1", 11211);
memcache.connect(function() {
sys.debug("about to set node test key");
memcache.set("node_test_key", '127.0.0.1');
});
var server = http.createServer(function (req, res) {
// I can read the data here, but then I need to buffer it.
//
//memcache.set("node_test_key", '127.0.0.1');
var reqEnded = false;
// this hopefully stops the flow of data until we determine the upstream, and
// will catch and flag the end event if it happens before we get a chance to lisen
// with access to the client
req.pause();
req.addListener("end", function() {
// we are done sending - close and flush client
reqEnded = true;
});
pick_server(req, function(host, port) {
var conn = http.createClient(port, host);
var headers = req.headers;
var client = conn.request(req.method, req.url, req.headers);
// I'd like to listen for +data on 'req' here.
// You can!
req.addListener("data", function(chunk) {
client.write(chunk, "binary");
});
// however, listening for data didn't work until this was added.
req.addListener("end", function() {
// we are done sending - close and flush client
client.close();
});
req.resume(); // once the listeners are bound, we are good
// If the request ended before the server was picked, close the client and go for
// it.
if (reqEnded)
client.close();
// this is being invoked multiple times with couch. It shouldn't be, according
// to the docs, however the HTTP responses coming back imply it should
client.addListener("response", function (resp) {
if (resp.statusCode == 100) {
// this is a continue, we will anyway thankyou - so don't send to client
// Seriously though, look in to this more - I'm not sure ignoring is OK, but
// it seems right
return;
}
res.writeHeader(resp.statusCode, resp.headers);
resp.addListener("data", function (chunk) {
res.write(chunk, "binary");
});
// to wrap it up
resp.addListener('end', function () {
// close the response
res.close();
});
});
});
});
// run
server.listen(4040);
// Mock version, talking to couch.
function pick_server(req, callback) {
memcache.connect(function() {
memcache.get('node_test_key', function (result) {
callback(result, '5984');
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment