我正在阅读这篇文章: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();
});
我何时或为什么要使用流?