使用.pipe()时node.js fs.createReadStream错误

时间:2014-02-28 01:24:11

标签: javascript node.js

使用此功能,我试图将输入呈现为文件

function Write(err, content, output) {
    if (err) { console.error(err) };
    var fs = require('fs');
    var inp = fs.createReadStream(content);
    var out = fs.createWriteStream(output);
    inp.pipe(out);
}

例如......

write('some text', 'write-test.txt');

然而,运行以上作为测试我得到以下返回

2) The Write module should write to test.txt:
     TypeError: path must be a string
      at Object.fs.open (fs.js:418:11)
      at open (/Users/joshburns/Code/micro-format/node_modules/grunt/node_modules/rimraf/node_modules/graceful-fs/graceful-fs.js:60:16)
      at Object.gracefulOpen [as open] (/Users/joshburns/Code/micro-format/node_modules/grunt/node_modules/rimraf/node_modules/graceful-fs/graceful-fs.js:45:3)
      at WriteStream.open (fs.js:1654:6)
      at new WriteStream (fs.js:1644:10)
      at Object.fs.createWriteStream (fs.js:1608:10)
      at Write (/Users/joshburns/Code/micro-format/lib/write.js:9:450)
      at Context.<anonymous> (/Users/joshburns/Code/micro-format/test/write/index.js:8:4)
      at Test.Runnable.run (/Users/joshburns/Code/micro-format/node_modules/grunt-mocha-test/node_modules/mocha/lib/runnable.js:211:32)
      at Runner.runTest (/Users/joshburns/Code/micro-format/node_modules/grunt-mocha-test/node_modules/mocha/lib/runner.js:358:10)

1 个答案:

答案 0 :(得分:0)

示例中的第一个参数是err而不是content,而outputundefined,这是错误消息的原因。

另外:阅读文档:fs.createReadStream需要文件名,而不是内容。您可能需要的是fs.writeFilefs.openfs.write相结合。