Skip to content

Instantly share code, notes, and snippets.

@s3u
Created May 16, 2012 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s3u/2712347 to your computer and use it in GitHub Desktop.
Save s3u/2712347 to your computer and use it in GitHub Desktop.
var http = require('http'),
EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
var server = http.createServer(function (req, res, proxy) {
emitter.on('done', function (e) {
if(e) {
res.writeHead(502);
res.end();
}
else {
res.writeHead(200);
res.end();
}
})
var options = {
host: 'localhost',
port: 8000,
path: '/someslowresource',
method: 'GET'
};
var happy = false;
var clientReq = http.request(options, function (clientRes) {
happy = true;
emitter.emit('done');
});
clientReq.setTimeout(1200, function () {
if(happy) {
console.log('in timeout');
}
emitter.emit('done', {
message: 'timeout'
});
});
clientReq.on('error', function () {
console.log('in error');
clientReq.connection.destroy();
emitter.emit({
message: 'error'
});
})
clientReq.end();
});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment