Created
February 23, 2012 03:02
-
-
Save c4milo/1889610 to your computer and use it in GitHub Desktop.
Unexpected http.request behavior.
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 headers = { | |
'x-apikey': 'blah' | |
//'connection': 'keep-alive' | |
}; | |
var options = { | |
host: 'localhost', | |
port: 1337, | |
headers: headers, | |
method: 'POST', | |
path: '/' | |
}; | |
var req = http.request(options, function(res) { | |
res.on('data', function (chunk) { | |
console.log(chunk + ''); | |
}); | |
}); | |
req.on('error', function(error) { | |
console.log(error); | |
}); | |
req.on('socket', function(socket) { | |
var interval; | |
socket.on('connect', function() { | |
var i = 0; | |
//Uncommeting setInterval block makes it work as expected. | |
//interval = setInterval(function() { | |
i++; | |
req.write(i + ' - yo client!\n'); | |
//}, 0); | |
}); | |
socket.on('close', function() { | |
clearInterval(interval); | |
console.log('socket closed'); | |
}); | |
}); | |
req.write('\n'); |
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) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
var interval = setInterval(function() { | |
console.log('sending data...'); | |
res.write('Hello World\n'); | |
}, 1000); | |
req.on('close', function() { | |
console.log('client request closed'); | |
clearInterval(interval); | |
}); | |
req.on('data', function(chunk) { | |
console.log(chunk + ''); | |
}); | |
}).listen(1337, "127.0.0.1"); | |
console.log('Server running at http://127.0.0.1:1337/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment