[Node.js] Level 3 new. Steam

File Read Stream

Lets use the fs module to read a file and log its contents to the console.

Use the fs module to create a Readable stream for fruits.txt. Store the new stream in a variable called file.

fs.createReadStream('fruits.txt');

Next, listen to the readable event on the newly created stream and give it a callback.

file.on('readable', function(){});

Inside the callback, read the data chunks from the stream and print them to the console using console.log() - you might want to use a while loop to do this. Don't forget to call toString() on the data before printing it.

file.on('readable', function(){
  while(null !== (chunk = file.read())){
      console.log(chunk.toString());
  }
});

 

var fs = require('fs');
var file = fs.createReadStream('fruits.txt');

file.on('readable', function(){
  while(null !== (chunk = file.read())){
      console.log(chunk.toString());
  }
});

 

File Piping

Instead of manually listening for the 'readable' event on theReadable stream, let's use pipe to read from the stream and write directly to process.stdout.

Start by removing the code for the readable handler.

 

Call file.pipe(), passing it the stream to write to.

var file = fs.createReadStream('fruits.txt');
file.pipe(process.stdout);

Read More: http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options

For example, emulating the Unix cat command:

process.stdin.pipe(process.stdout);

 

var fs = require('fs');

var file = fs.createReadStream('fruits.txt');
file.pipe(process.stdout);

 

Fixing Pipe

The following code will throw an error because pipe automatically closed our writable stream.

You'll need to consult the pipe documentation to figure out the option which keeps the Write stream open and dispatches the end event.

  By default end() is called on the destination when the source stream emits end, so that destination is no longer writable. Pass{ end:   false } as options to keep the destination stream open.

file.pipe(destFile, { end: false });

 

复制代码
var fs = require('fs');

var file = fs.createReadStream('origin.txt');
var destFile = fs.createWriteStream('destination.txt');

file.pipe(destFile, { end: false });

file.on('end', function(){
  destFile.end('Finished!');
});
复制代码

 

Download Server

Let's create an HTTP server that will serve index.html.

Use pipe() to send index.html to the response.

复制代码
var fs = require('fs');
var http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/html'});

  var file = fs.createReadStream('index.html');
  file.pipe(response);
}).listen(8080);
复制代码

 

 

posted @   Zhentiw  阅读(890)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示