Nodejs请求管道终止响应

时间:2016-06-23 15:49:13

标签: javascript node.js

我有这个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响应?

1 个答案:

答案 0 :(得分:0)

如果你管道到一个流,那么一旦源结束就会关闭目的地(期望你传递一个options回调作为选项)

readable.pipe(destination[, options])

  

<Object> end管道选项
      <Boolean> true当读者结束时结束作者。默认为// 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(); }

这里是源代码中的相应部分 node: stream.js

def for_token(_),  do: {:ok, "ok"}    
def from_token(_), do: {:ok, "ok"}