简体   繁体   English

Node.js请求管道终止响应

[英]Nodejs request pipe terminates response

I have this nodejs code but to be honeste I can't quite understand what .pipe does. 我有这个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 in this case is the response from a function that handles a specific route. 在这种情况下,res是来自处理特定路由的功能的响应。

Does .pipe in this case end the res response with the response it gets? 在这种情况下,.pipe是否以得到的响应结束res响应?

If you pipe to a stream then the destination will be closed as soon as the source ends (expect if you pass an end callback as option) 如果通过管道传输到流,则源将在源结束时立即关闭目标(请注意是否将end回调作为选项传递)

readable.pipe(destination[, options]) : 可读.pipe(目的地[,选项])

options <Object> Pipe options options <Object>管道选项
end <Boolean> End the writer when the reader ends. end <Boolean>当阅读器结束时结束编写器。 Defaults to true . 默认为true

And here the corresponding part in the source node: stream.js 这里是源节点中的对应部分: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