Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created July 29, 2011 23:24
Show Gist options
  • Save bnoordhuis/1114958 to your computer and use it in GitHub Desktop.
Save bnoordhuis/1114958 to your computer and use it in GitHub Desktop.
http = require('http');
fs = require('fs');
FRONTEND_PORT = 8000;
BACKEND_PORT = 8001;
//CONTENT_LENGTH = 2147483648; // 2 GB
CONTENT_LENGTH = 2097152; // 2 MB
function log_events(prefix, obj) {
var fun = obj.emit;
obj.emit = function(what, b, c, d, e) {
if (what !== 'newListener') {
console.error(prefix, arguments);
}
return fun.call(obj, what, b, c, d, e);
};
}
function log_events(prefix, obj) {}
function frontend(req, res) {
res.writeHead(200, {
'Content-Length': '' + CONTENT_LENGTH,
'Content-Type': 'application/octet-stream',
'Connection': 'close'
});
res._send(''); // force flush
var options = {
method: 'GET',
host: '127.0.0.1',
port: BACKEND_PORT,
path: '/'
};
var client_req = http.request(options, function(stream) {
stream.pipe(res);
});
log_events('frontend req', req);
log_events('frontend res', res);
log_events('frontend client req', client_req);
client_req.end();
}
function backend(req, res) {
res.writeHead(200, {
'Content-Length': '' + CONTENT_LENGTH,
'Content-Type': 'application/octet-stream',
'Connection': 'close'
});
res._send(''); // force flush
var stream = fs.createReadStream('/dev/zero', {
start: 0,
end: CONTENT_LENGTH
});
log_events('backend req', req);
log_events('backend res', res);
log_events('backend stream', stream);
stream.pipe(res);
}
http.createServer(frontend).listen(FRONTEND_PORT);
http.createServer(backend).listen(BACKEND_PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment