【发布时间】:2012-09-08 18:26:48
【问题描述】:
我正在阅读这篇文章:http://elegantcode.com/2011/04/06/taking-baby-steps-with-node-js-pumping-data-between-streams/ 并且在理解流时遇到了一些小问题。
引用:
"Suppose we want to develop a simple web application that reads a particular file from disk and send it to the browser. The following code shows a very simple and naïve implementation in order to make this happen."
所以代码示例如下:
var readStream = fileSystem.createReadStream(filePath);
readStream.on('data', function(data) {
response.write(data);
});
readStream.on('end', function() {
response.end();
});
既然我们可以简单地做,为什么还要使用上述方式:
fs.readFile(filePath, function(err, data){
response.write(data);
response.end();
});
我何时或为什么要使用流?
【问题讨论】: