Reputation: 1750
I have this nodejs code but to be honeste I can't quite understand what .pipe does.
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.
Does .pipe in this case end the res response with the response it gets?
Upvotes: 1
Views: 394
Reputation: 40842
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)
readable.pipe(destination[, options]):
options
<Object>
Pipe options
end
<Boolean>
End the writer when the reader ends. Defaults totrue
.
And here the corresponding part in the source node: 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();
}
Upvotes: 1