I'm trying to open an img file and send it through http. I have read that the best method for doing it is creatReadStream because it's async and better for performance.
Here it's my code:
var file = fs.createReadStream(image);
file.on("error", function(err){
...
});
res.writeHead(200, {"Content-Type" : "image/png"});
file.on("data", function(data) {
res.write(data);
})
file.on("end", function() {
res.end();
})
How can I know the file size to let res know in order to send it in the headers?
I don't like having writehead not in a callback, what can I do?
Thank you.