繁体   English   中英

Node.js请求管道终止响应

[英]Nodejs request pipe terminates response

我有这个nodejs代码,但是老实说我不太明白.pipe的作用。

var localoptions = {
  url:  requestUrl,
  milliseconds, default is 2 seconds
};


if (authHeader) {
  localoptions.headers = {
    "Authorization": authHeader
  };
}

request(localoptions)
.on('error', function(e) {
 res.end(e);
}).pipe(res);

在这种情况下,res是来自处理特定路由的功能的响应。

在这种情况下,.pipe是否以得到的响应结束res响应?

如果通过管道传输到流,则源将在源结束时立即关闭目标(请注意是否将end回调作为选项传递)

可读.pipe(目的地[,选项])

options <Object>管道选项
end <Boolean>当阅读器结束时结束编写器。 默认为true

这里是源节点中的对应部分:stream.js

// If the 'end' option is not supplied, dest.end() will be called when
// source gets the 'end' or 'close' events.  Only dest.end() once.
if (!dest._isStdio && (!options || options.end !== false)) {
   source.on('end', onend);
   source.on('close', onclose);
}

var didOnEnd = false;
function onend() {
   if (didOnEnd) return;
   didOnEnd = true;

   dest.end();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM