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);

[Node.js] Level 3 new. Steam的更多相关文章

  1. [Node.js] Level 7. Persisting Data

    Simple Redis Commands Let's start practicing using the redis key-value store from our node applicati ...

  2. [Node.js] Level 6. Socket.io

    6.2 Setting Up socket.io Server-Side So far we've created an Express server. Now we want to start bu ...

  3. [Node.js] Level 2 new. Event

    Chat Emitter We're going to create a custom chat EventEmitter. Create a new EventEmitter object and ...

  4. [Node.js] Level 5. Express

    Express Routes Let's create an express route that accepts GET requests on'/tweets' and responds by s ...

  5. A chatroom for all! Part 1 - Introduction to Node.js(转发)

    项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...

  6. Node.js开发者最常范的10个错误

    目录 前言 1 不使用开发工具 1.1 自动重启工具 1.2 浏览器自动刷新工具 2 阻塞event loop 3 频繁调用回调函数 4 圣诞树结构的回调(回调的地狱) 5 创建一个大而完整的应用程序 ...

  7. Node.js的线程和进程

    http://www.admin10000.com/document/4196.html 前言 很多Node.js初学者都会有这样的疑惑,Node.js到底是单线程的还是多线程的?通过本章的学习,能够 ...

  8. Windows下Node.js+Express+WebSocket 安装配置

    Linux参考: Linux安装Node.js 使用Express搭建Web服务器 Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V ...

  9. 为什么 Node.js 这么火,而同样异步模式 Python 框架 Twisted 却十几年一直不温不火?

    twisted是一个强大的异步网络框架,应用的面也非常广,但是没有这几年才出现的Node.js火,社区.文档也是很少可怜我觉得二者其实在本质上差不多,而且python使用起来还是比较容易一些的 匿名用 ...

随机推荐

  1. luoguP3979 遥远的国度 树链剖分

    \(1, 2\)操作没什么好说的 对于\(3\)操作,分三种情况讨论下 \(id = rt\)的情况下,查整棵树的最小值即可 如果\(rt\)在\(1\)号点为根的情况下不在\(id\)的子树中,那么 ...

  2. ZOJ2112 BZOJ1901 Dynamic Rankings 树套树 带修改的区间第k小

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 树套树,线段树套splay或者主席树套树状数组,我抄了一下hzwer ...

  3. Windows 安装 Jenkins 2.6

    最近都是Windows下干活啊... 一.下载和安装 官网地址:https://jenkins.io/index.html,选择了2.X系列的Windows版本,自动在浏览器下载到了jenkins-2 ...

  4. 【BZOJ-3110】K大数查询 整体二分 + 线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Sta ...

  5. Codeforces Round #353 (Div. 2) B. Restoring Painting 水题

    B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...

  6. 如何编写LVS对Real Server的健康状态检测脚本

    简介:Linux 虚拟服务器(Linux Virtual Server. LVS),是一个由章文松开发的自由软件.利用KVS可以实现高可用的.可伸缩缩的Web, Mail, Cache和Medial等 ...

  7. Android 5.0 源代码结构

    本节书摘来自异步社区<深入理解Android 5 源代码>一书中的第2章,第2.2节分析Android源代码结构,作者 李骏. 网址:https://yq.aliyun.com/artic ...

  8. [置顶] iOS中让省略号垂直居中

    在显示等待框时,一般要求在提示信息后面加个省略号,但中文输入法下输入的省略号是在底部对齐,但中 文的习惯是省略号垂直居中对齐,最后找到下面这个方法来显示垂直居中的省略号: 中文和英文输入法下一样: o ...

  9. iOS开源控件库收集

    下拉刷新 将数据保存至keyRing 简单的模板引擎,用来生成html OmniGroup 这个其实不是类库,是一个Cocoa的Recipe CocoaPods 为XCode project提供一个类 ...

  10. arcgis10.5新功能图形缓冲

    摘要 在输入要素周围某一指定距离内创建缓冲区多边形.在要素周围生成缓冲区时,大部分制图形状对缓冲区末端(端头)和拐角(连接)可用. 插图