流没有结束(没有发送响应)

Stream is not ending (response is not being sent)

我有这个,非常接近工作:

app.post('/html-to-pdf', (req,res,next) => {

  log.info('got request to convert html to pdf...');

  const k = cp.spawn('pandoc', ['-f', 'html','--pdf-engine=xelatex','-t','latex']);
  req.pipe(k.stdin, {end: true}).pipe(res, {end:true});

  k.stdout.on('data', d => {
    console.log('stdout:', String(d));
  });

  res.on('data', d => {
    console.log('data:',d);
  });

});

我可以发送路由 html 使用:

$ curl -d '<html>hiiii</html>' -X POST localhost:3701/html-to-pdf

服务器将获取数据并进行转换,此处理程序将打印正确的信息:

  k.stdout.on('data', d => {
    console.log('stdout:', String(d)); // logs "hiiii"
  });

问题 是永远不会发送响应!它只是永远挂起。如您所见,我在两个管道调用中都使用了 {end:true},但这没有帮助。

我想出来了,因为我应该这样做:

req.pipe(k.stdin);
k.stdout.pipe(res);

而不是这个:

req.pipe(k.stdin).pipe(res);

我怀疑在这种情况下是否需要明确的 {end:true},但我没有测试它。