Created
May 28, 2011 13:39
-
-
Save pfleidi/996866 to your computer and use it in GitHub Desktop.
Create custom timeouts for Node.js http.ClientRequest
This file contains 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"); | |
var TIMEOUT_VALUE = 2020; | |
var options = { | |
host: 'localhost', | |
port: 3000, | |
path: '/', | |
method: 'GET' | |
}; | |
var timeoutId; | |
var req = http.request(options, function (response) { | |
var data = ''; | |
clearTimeout(timeoutId); | |
response.on('data', function (chunk) { | |
data += chunk; | |
}); | |
response.on('end', function () { | |
console.log(data); | |
}); | |
}); | |
timeoutId = setTimeout(function () { | |
console.log('TIMEOUT TRIGGERED'); | |
req.abort(); | |
}, TIMEOUT_VALUE); | |
req.on('error', function (err) { | |
console.log('ERROR: ' + err.message); | |
}); | |
req.end(); |
This file contains 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'); | |
http.createServer(function (req, res) { | |
setTimeout(function () { | |
res.end('HELLO WOLD!'); | |
}, 2000); | |
}).listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment