Skip to content

Instantly share code, notes, and snippets.

@c4milo
Created February 23, 2012 03:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c4milo/1889610 to your computer and use it in GitHub Desktop.
Save c4milo/1889610 to your computer and use it in GitHub Desktop.
Unexpected http.request behavior.
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');
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