Skip to content

Instantly share code, notes, and snippets.

@pfleidi
Created May 28, 2011 13:39
Show Gist options
  • Save pfleidi/996866 to your computer and use it in GitHub Desktop.
Save pfleidi/996866 to your computer and use it in GitHub Desktop.
Create custom timeouts for Node.js http.ClientRequest
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();
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