[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);
[Node.js] Level 3 new. Steam的更多相关文章
- [Node.js] Level 7. Persisting Data
Simple Redis Commands Let's start practicing using the redis key-value store from our node applicati ...
- [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 ...
- [Node.js] Level 2 new. Event
Chat Emitter We're going to create a custom chat EventEmitter. Create a new EventEmitter object and ...
- [Node.js] Level 5. Express
Express Routes Let's create an express route that accepts GET requests on'/tweets' and responds by s ...
- A chatroom for all! Part 1 - Introduction to Node.js(转发)
项目组用到了 Node.js,发现下面这篇文章不错.转发一下.原文地址:<原文>. ------------------------------------------- A chatro ...
- Node.js开发者最常范的10个错误
目录 前言 1 不使用开发工具 1.1 自动重启工具 1.2 浏览器自动刷新工具 2 阻塞event loop 3 频繁调用回调函数 4 圣诞树结构的回调(回调的地狱) 5 创建一个大而完整的应用程序 ...
- Node.js的线程和进程
http://www.admin10000.com/document/4196.html 前言 很多Node.js初学者都会有这样的疑惑,Node.js到底是单线程的还是多线程的?通过本章的学习,能够 ...
- Windows下Node.js+Express+WebSocket 安装配置
Linux参考: Linux安装Node.js 使用Express搭建Web服务器 Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V ...
- 为什么 Node.js 这么火,而同样异步模式 Python 框架 Twisted 却十几年一直不温不火?
twisted是一个强大的异步网络框架,应用的面也非常广,但是没有这几年才出现的Node.js火,社区.文档也是很少可怜我觉得二者其实在本质上差不多,而且python使用起来还是比较容易一些的 匿名用 ...
随机推荐
- 清北冬令营入学测试[ABCDEF]
http://tyvj.cn/Contest/861 [1.2.2017] 像我这种蒟蒻只做了前6道还有道不会只拿了暴力分 A 描述 这是一道有背景的题目,小A也是一个有故事的人.但可惜的是这里纸张太 ...
- Cipolla算法学习小记
转自:http://blog.csdn.net/doyouseeman/article/details/52033204 简介 Cipolla算法是解决二次剩余强有力的工具,一个脑洞大开的算法. 认真 ...
- ACM需要掌握算法
数据结构 栈,队列,链表 哈希表,哈希数组 堆,优先队列 双端队列 可并堆 左偏堆 二叉查找树 Treap 伸展树 并查集 集合计数问题 二分图的识别 平衡二叉树 二叉排序树 线段树 一维线段树 二维 ...
- UVA 11945 Financial Management 水题
Financial Management Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 acm.hust.edu.cn/vjudge/problem/vis ...
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
Running Median Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- Python的静态方法和类成员方法
http://www.cnblogs.com/2gua/archive/2012/09/03/2668125.html Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还 ...
- generator自动生成mybatis的xml配置
generator自动生成mybatis的xml配置.model.map等信息:1.下载mybatis-generator-core-1.3.2.jar包. 网址:http://code. ...
- 《TCP/IP具体解释卷2:实现》笔记--IP:网际协议
本章介绍IP分组的结构和主要的IP处理过程,包含输入,转发和输出. 下图显示了IP层常见的组织形式. 在之前的文章中.我们看到了网络接口怎样把到达的IP分组放到IP输入队列ipintrq中去,并怎样调 ...
- C#中汉字排序简单示例(拼音/笔划)
可以按照区域语言修改排序规则. class Program { static void Main(string[] args) { string[] arr = { "趙(ZHAO)&quo ...
- SQL 条件 判断 select case as
" then "返回的数据1" " then "返回的数据2" else "返回的其他数据" end as 新的列名 f ...