Reputation: 2873
I have this code. The end
event is fired two times. I can't understand why. Any hints? Thanks.
var chance = require('chance').Chance();
require('http').createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
function generateMore() {
while(chance.bool({likelihood: 95})) {
var shouldContinue = res.write(
chance.string({length: (16 * 1024) - 1})
);
if(!shouldContinue) {
console.log('Backpressure');
return res.once('drain', generateMore);
}
}
res.end('\nThe end...\n', function() {
console.log('All data was sent'); // I see this log two times
});
}
generateMore();
}).listen(8080, function () {
console.log('Listening');
});
Upvotes: 1
Views: 128
Reputation: 7077
Because when you open URL, browser tries get favicon.ico and sends two requests to your server.
Upvotes: 4