Node.js/stream:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第168行: 第168行:
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
fs.createReadStream('/path/to/source').pipe(fs.createWriteStream('/path/to/target'));</syntaxhighlight>
fs.createReadStream('/path/to/source').pipe(fs.createWriteStream('/path/to/target'));</syntaxhighlight>
=== 流的复用 ===
将一个流进行复用。
{{leftrightflex|<syntaxhighlight lang="javascript">
const Readable = require('stream').Readable;
const stream = require('stream');
const rs = new Readable();
rs.push('kangkk\n');
rs.push(null);
const foo = rs.pipe(new stream.PassThrough());
const bar = rs.pipe(new stream.PassThrough());
foo.pipe(process.stdout);
bar.pipe(process.stdout);</syntaxhighlight>|{{consoleout|<pre>
kangkk
kangkk
</pre>}}}}


== 技巧 ==
== 技巧 ==