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);
  • 相关阅读:
    css基本的东西
    css写一个梯形
    js事件代理
    css的垂直居中
    css的6中居中的方式
    css的小三角实现的方式
    css清楚浮动的几种常用方法
    css中的伪类和伪元素
    js回调
    11.10 chkconfig:管理开机服务
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4070230.html
Copyright © 2011-2022 走看看