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. Java 的类加载顺序

    Java 的类加载顺序 一.加载顺序:先父类后子类,先静态后普通 1.父类的静态成员变量初始化 2.父类的静态代码块 3.子类的静态成员变量初始化 4.子类的静态代码块 5.父类的普通成员变量初始化 ...

  2. [Luogu5162]WD与积木(多项式求逆)

    不要以为用上Stirling数就一定离正解更近,FFT都是从DP式本身出发的. 设f[i]为i个积木的所有方案的层数总和,g[i]为i个积木的方案数,则答案为$\frac{f[i]}{g[i]}$ 转 ...

  3. OD基本汇编指令

    jmp ;无条件跳转 指哪飞哪 一些杂志中说的直飞光明顶,指的就是它了~ 光明顶一般指爆破地址根据条件跳转的指令:JE ;等于则跳转 JNE ;不等于则跳转 JZ ;为 0 则跳转   JNZ ;不为 ...

  4. bzoj 2244

    没有正确分析路径可能的条数,它是指数增长的,会爆long long. 然后就是正反两次时间分治. 另一个就是max with count,即带计数的最值,即除了记录最值,还要记录最值取得的次数. /* ...

  5. the elements of computing systems 的读书笔记2

    懒癌发作,本来计划是两到三天就一个unit的,没想到一直拖到今天才完成第二部分(6-8章). 第6章,介绍了hack汇编到二进制,也就是用翻译到01来表示.从课后习题来看,这一章目的就是设计一个程序( ...

  6. 网络服务器搭建的那些事(PV QPS Throughput) 转载

    一.前言: 从事后台sever开发的同学,代码开发完成之后,上线之前,总会进行各种黑盒白盒测试,压测.正确性测试... 而测试同学,会给开发同学一份测试报告,需要开发同学进行确认...问题来了,里面好 ...

  7. Git_搭建Git服务器

    在远程仓库一节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想 ...

  8. poj 1279 Art Gallery - 求多边形核的面积

    /* poj 1279 Art Gallery - 求多边形核的面积 */ #include<stdio.h> #include<math.h> #include <al ...

  9. android aapt 用法 -- ApkReader

    aapt 是android assert packaging tool的缩写,具体如下: 1. 列出apk包的内容 aapt l[ist] [-v] [-a] file.{zip,jar,apk} - ...

  10. 在Powerdesigner中创建概念数据模型

    点击菜单“File”---->“New Model” 点击[OK]按钮后,将进入如下的画面 系统将出现一个工具栏如下,用于在设计面板中设计模型