如何在节点中流入/流出文件描述符?

时间:2014-07-05 01:21:25

标签: node.js stream file-descriptor child-process

fs.createReadStream()fs.createWriteStream()仅支持文件路径,但我需要从文件描述符读取(或写入)(传递给子进程/从子进程传递)。 / p>

注意我需要Streams,因此fs.open/fs.read/fs.write是不够的。

2 个答案:

答案 0 :(得分:14)

当您致电fs.createReadStream时,您可以传入文件描述符:

var fs = require('fs');
var fd = fs.openSync('/tmp/tmp.js', 'r');
var s = fs.createReadStream(null, {fd: fd});
s.pipe(process.stdout);

如果有fd选项,则忽略文件名。

答案 1 :(得分:1)

// Open &3:
process.oob1 = fs.createWriteStream(null, { fd: 3 });
// Write to &3 / oob1 (out-of-band 1)
process.oob1.write("Note: this will throw an exception without 3>&1 or something else declaring the existence of &3");