Node.js 使用NodeJS Express服务器发送直通流响应时出错

Node.js 使用NodeJS Express服务器发送直通流响应时出错,node.js,express,nodejs-stream,Node.js,Express,Nodejs Stream,我试图传递一个nodejsstreamapiPassThrough对象作为对http请求的响应。我正在运行一台Express服务器,正在执行类似于以下操作: const { PassThrough } = require('stream') const createPassThrough = () => { return PassThrough() } app.get('/stream', (req, res) => { res.writeHead(200, {

我试图传递一个nodejsstreamapi
PassThrough
对象作为对http请求的响应。我正在运行一台Express服务器,正在执行类似于以下操作:

const { PassThrough } = require('stream')

const createPassThrough = () => {
  return PassThrough()
}

app.get('/stream', (req, res) => {
  res.writeHead(200, {
    'Content-Type': 'audio/mpeg',
    'Transfer-Encoding': 'chunked'
  })
  res.write(createPassThrough())
})
但是,当我尝试执行此操作时,Express会抛出以下错误:

The first argument must be of type string or an instance of Buffer. Received an instance of PassThrough
我是否需要使用Express来实现这一点,还是需要使用不同的框架?我已经读到,
Hapi.js
能够返回一个
PassThrough
对象。

stream write()操作旨在写入数据块,而不是对可读源的引用


看起来,
passThrough.pipe(res)
可能是OP想要实现的。这将把写入直通的所有数据传播到Express响应中。

。谢谢我将看看如何重构代码,以便将
res
传递回最初传递
传递的函数。我还设法让它在
Hapi.js
上运行得很好,但我想改用Express,因为我更熟悉它。