zoukankan      html  css  js  c++  java
  • [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);
  • 相关阅读:
    base64模块的使用
    14-类的结构之一:属性
    13-类和对象
    12-数组的常见异常
    11-Arrays工具类的使用
    10-二维数组
    09-一维数组
    08-数组的概述
    07-流程控制
    06-运算符
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4070230.html
Copyright © 2011-2022 走看看