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. 【BZOJ 1419】1419: Red is good (概率DP)

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 807  Solved: 343 Description 桌面上有R张 ...

  2. BZOJ3207 花神的嘲讽计划

    hash值建主席树. 垃圾题面没有熟虑范围害我MLE——>RE. By:大奕哥 #include<bits/stdc++.h> #define unll unsigned long ...

  3. 「NOI2014」购票

    「NOI2014」购票 解题思路 先列出 \(dp\) 式子并稍微转化一下 \[ dp[u] =\min(dp[v]+(dis[u]-dis[v]) \times p[u] + q[u])) \ \ ...

  4. [POI2017]Sabotaż

    [POI2017]Sabotaż 题目大意: 一棵\(n(n\le5\times10^5)\)个结点的树,初始时有一个未知的黑点,其余全为白点.对于一个点,如果其子树中黑点所占比例超过\(x\),则这 ...

  5. SQL SERVER 2008 多边形问题的解决

    报错内容: 在执行用户定义例程或聚合 "geometry" 期间出现 .NET Framework 错误: System.ArgumentException: 24144: 由于该 ...

  6. Express应用程序目录结构

    1.Node安装与使用 网上有很多Node的安装教程,可以做参考 2.MongoDB的安装与使用 MongoDB安装也有很多教程,可以去网上找找 3.初始化一个express项目 使用express框 ...

  7. Codeforces Round #222 (Div. 1) D. Developing Game 扫描线

    D. Developing Game 题目连接: http://www.codeforces.com/contest/377/problem/D Description Pavel is going ...

  8. vue 组件开发 props 验证

    使用props 在Vue中父组件向子组件中传送数据是通过props实现的,一个简单的使用props的例子: <!DOCTYPE html> <html> <head> ...

  9. BTSync FREE vs BTSync PRO

    Although both BitTorrent Sync 2.0 FREE and PRO ensure high file transfer speed and ultimate security ...

  10. 网络编程_Python-网络模型.

    http://xmdevops.blog.51cto.com/11144840/1861280