var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
var stream = fs.createReadStream(__dirname + '/data.txt');
stream.pipe(res);
});
server.listen(8000);
每次讀到一小段數據後隨即發送給客戶
在cmd輸入node再輸入stream,觀看stream的方法
第一個
Readable
var Readable = require('stream').Readable;
var rs = new Readable;
rs.push('beep ');
rs.push('boop\n');
rs.push(null);
rs.pipe(process.stdout);
rs.push(null); 告訴Readable要結束了
沒有在結尾輸入的話會產生錯誤
或使用
var Readable = require('stream').Readable;
var rs = new Readable;
rs.push('Hi ');
rs.push('Hello\n');
rs.push('World\n');
rs.push(null);
rs.on('data', function(chunk) {
console.log(chunk);
});
但只讀到buffer,如何轉為字串?
var Readable = require('stream').Readable;
var rs = new Readable;
rs.setEncoding('utf8');
rs.push('Hi ');
rs.push('Hello\n');
rs.push('World\n');
rs.push(null);
rs.on('data', function(chunk) {
console.log(chunk);
});
上面是開啟監聽data,如果想在結束後才一次讀出呢?
監聽end
var Readable = require('stream').Readable;
var aa;
var rs = new Readable;
rs.setEncoding('utf8');
rs.push('Hi ');
rs.push('Hello\n');
rs.push('World\n');
rs.push(null);
rs.on('data', function(chunk) {
aa+=chunk;
});
rs.on('end',function(){
console.log(aa);
})
如何讓他暫停不要結束?
pause();
var Readable = require('stream').Readable;
var aa;
var rs = new Readable;
rs.setEncoding('utf8');
rs.push('Hi ');
rs.push('Hello\n');
rs.push('World\n');
rs.push(null);
rs.on('data', function(chunk) {
aa+=chunk;
});
rs.pause();
rs.on('end',function(){
console.log(aa);
})
Writable: streams to which data can be written (for example, fs.createWriteStream()).
Readable: streams from which data can be read (for example, fs.createReadStream()).
Duplex: streams that are both Readable and Writable (for example, net.Socket).
Transform: Duplex streams that can modify or transform the data as it is written and read (for example, zlib.createDeflate()).
In any scenario where the data buffer has exceeded the [highWaterMark][] or the write queue is currently busy, [.write()][] will return false.
When a false value is returned, the backpressure system kicks in. It will pause the incoming [Readable][] stream from sending any data and wait until the consumer is ready again. Once the data buffer is emptied, a ['drain'][] event will be emitted and resume the incoming data flow.