我試圖打開一個img文件並通過http發送它。我已經讀過,最好的方法是creatReadStream,因爲它是異步的,性能更好。createReadStream發送文件http
這是我的代碼:
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();
})
我怎樣才能知道文件的大小,讓水庫知道爲了把它的頭?
我不喜歡在回調中寫頭,我該怎麼辦?
謝謝。
我回復了你作爲自己的答案,因爲代碼。 – Mario