Last active
May 28, 2016 17:59
-
-
Save evantahler/2f6c4241c47d7c89f5555d833475c8b8 to your computer and use it in GitHub Desktop.
Exploring a node.js memory leak with sending files and setting the Content-Length header
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 fs = require('fs'); | |
var http = require('http'); | |
var file = __dirname + '/index.html'; | |
var connections = {}; | |
var idCouner = 0 | |
var port = 8080; | |
var handleRequset = function(request, response){ | |
idCouner++ | |
var id = idCouner; | |
connections[id] = {req: request, res: response}; | |
response.on('finish', function(){ | |
delete connections[id]; | |
}); | |
fs.stat(file, function(error, stats){ | |
if(error){ throw error; } | |
response.writeHead(200, [['Content-Length', stats.size]]); | |
var fileStream = fs.createReadStream(file); | |
fileStream.on('open', function(){ | |
fileStream.pipe(response); | |
}); | |
fileStream.on('error', function(error){ | |
console.log(error); // no errors are caught | |
}); | |
}); | |
}; | |
http.createServer(handleRequset).listen(port); | |
console.log('server running on port ' + port); | |
setInterval(function(){ | |
console.log('connections: ' + Object.keys(connections)); | |
}, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please move conversation about this issue to nodejs/node#6929