Created
May 16, 2012 17:18
-
-
Save s3u/2712347 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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