public
Created

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.