Skip to content

Instantly share code, notes, and snippets.

@watson
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watson/95b16cf24975d2f2a8fe to your computer and use it in GitHub Desktop.
Save watson/95b16cf24975d2f2a8fe to your computer and use it in GitHub Desktop.

This sets up two HTTP servers on port 3000 and 3001. The one on port 3000 works as expected, the one on port 3001 doesn't.

Console log output when requesting server on port 3000:

------- NEW WORKING REQUEST -------
New listener added: end
adding 1st data litener
New listener added: data
New listener added: data
New listener added: readable
New listener added: readable
foo
bar

Console log output when requesting server on port 3001:

------- NEW BROKEN REQUEST -------
New listener added: end
adding 1st data litener
New listener added: data
New listener added: data
New listener added: readable
foo
New listener added: readable
var http = require('http');
var listen = function (req, res) {
var listening = false;
req.on('newListener', function (event) {
console.log('New listener added:', event);
if (!listening && event === 'data') {
listening = true;
req.on('data', function () {
console.log('foo');
res.write('foo\n');
});
}
});
};
var works = function (req, res) {
console.log('------- NEW WORKING REQUEST -------');
listen(req, res);
req.on('end', function () {
res.end();
});
console.log('adding 1st data litener');
req.on('data', function () {
console.log('bar');
res.write('bar\n');
});
};
var broken = function (req, res) {
console.log('------- NEW BROKEN REQUEST -------');
listen(req, res);
req.on('end', function () {
res.end();
});
process.nextTick(function () {
console.log('adding 1st data litener');
req.on('data', function () {
console.log('bar');
res.write('bar\n');
});
});
};
http.createServer(works).listen(3000);
http.createServer(broken).listen(3001);
var http = require('http');
var listen = function (req, res) {
var listening = false;
req.on('newListener', function (event) {
console.log('New listener added:', event);
if (!listening && event === 'data') {
listening = true;
var size = Buffer.concat(req._readableState.buffer).length;
req.once('end', function () {
console.log('Total request size:', size);
});
var attachListener = function () {
req.on('data', function (chunk) {
size += chunk.length;
});
};
if (size)
process.nextTick(attachListener);
else
attachListener();
}
});
};
var sync = function (req, res) {
console.log('------- REQUEST: SYNC -------');
listen(req, res);
req.on('end', function () {
res.end();
});
console.log('adding 1st data litener');
req.on('data', function () {
process.stdout.write('.');
});
};
var async = function (req, res) {
console.log('------- REQUEST: ASYNC -------');
listen(req, res);
req.on('end', function () {
res.end();
});
process.nextTick(function () {
console.log('adding 1st data litener');
req.on('data', function () {
process.stdout.write('.');
});
});
};
http.createServer(sync).listen(3000);
http.createServer(async).listen(3001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment