NodeJS:Stream.pipe(Stream) 有效但 Stream.read() 无效

NodeJS: Stream.pipe(Stream) works but Stream.read() doesn't

我是 NodeJS 的新手,我基本上想做的是通过 HTTP 将 .pdf 上传到我的服务器。我正在使用一个 POST rquest 来获取 Content-Type multipart/form-data。在 NodeJS 中,我使用 multiparty 来解析我的请求。

有趣的是,当我想访问我的请求的表单数据部分时,在我的例子中是一个 JSON 对象,当我管道()我的流时它工作从多方获取标准输出但是当我读取()流时,我只得到空值。

这是我代码中(可能)重要的部分:

form.on('part', (part) => {    // part is the Stream returned by multiparty
  if(!part.filename) {         // only fields, not files
    console.log(part.read());  // output is null
    part.pipe(process.stdout); // output is my JSON object 
    part.resume();
  }
}

我没有收到任何错误。

非常抱歉,我犯了一个严重的转储错误...

提前致谢,

路易斯!

也许 part 处于流动模式。你可以试试这样的

form.on('part', (part) => {    // part is the Stream returned by multiparty
  if(!part.filename) {         // only fields, not files
    part.on('data', chunk => {
      console.log(chunk.toString())
    })
    .on('error', console.error) // if stream emits errors you should handle them
  }
}