通过管道将流传送到任何地方

alm*_*arc 5 javascript node.js

我有一个数据流,我需要将其写入任何地方,但又不能立即停止它。下面的代码写入一个文件,因此只要流正在运行,它就会保持连接处于活动状态。

 request
.get(href)
.on('response', function(response) {
    console.log(response.statusCode)
    console.log(response.headers['content-type'])
})
.pipe(fs.createWriteStream("name.mp3"))
Run Code Online (Sandbox Code Playgroud)

是否可以将该流传输到任何地方,同时仍然保持连接fs.createWritableStream

我尝试使用 dev-null (npm 包),但它似乎会立即终止连接。

Hei*_*tad 4

似乎是一个奇怪的问题,但也许你可以使用/dev/null,或者下面帖子中解释的 Windows 等效项?

如何从node.js写入Windows下的NUL设备?

我确实看到您尝试了与该dev-null包类似的操作,但也许可以在不使用该包的情况下尝试一下,看看是否有效。

  • .pipe(fs.createWriteStream("\\\\.\\NUL")) 帮助解决了这个问题。 (8认同)