learnyounode
learnyounode copied to clipboard
HTTP File Server: pipe not explained prior to this exercise
For the 11th exercise, "HTTP File Server," the official solution includes use of the pipe
method of a readable stream. The only prior mention of this method is as an optional solution to exercise 8 which suggests using the bl
and/or concat-stream
package. Within that exercise, there is no mention or explanation of the pipe
method, except as it is shown in the example code: response.pipe(bl(function (err, data) { /* ... */ }))
and the phrase, "Both bl and concat-stream can have a stream piped in to them and they will collect the data for you."
Because this method is required to complete the solution for exercise 11, as shown in part of the official solution: fs.createReadStream(process.argv[3]).pipe(res)
, I would say it leaves new learners at a frustrating disadvantage. For me personally, an integral part of the learning experience lies in debugging my own failed solutions with only the use of the node API docs and learnyounode's direct instructions. I feel it was not possible to understand that the pipe
method was required for exercise 11's solution without looking at a completed example, which to me is the equivalent of looking in the back of a math textbook for the answer to an exercise.
+1
+1
The documentation presented to the user can definitely be improved to mention the solution's use of pip and bl.
In the README file it is mentioned that this workshop builds on the stream-adventure workshop which has all the documentation and information you're seeking.
+1
Here's my solution without know the pipe existence. I had to dig into closure javascript universe for hours
var http = require('http');
var bl = require('bl');
var urls = process.argv.filter(function (arg, index) {
if (index > 1) return arg;
});
var pages = new Array(urls.length - 1);
var remaining_urls = urls.length;
handlePage = function (index) {
var buffer = new bl();
return function (response) {
response.on('data', function (data) {
buffer.append(data);
});
response.on('error', console.error);
response.on('end', function () {
pages[index] = buffer.toString();
if (--remaining_urls === 0) {
pages.forEach(function (page) {
console.log(page);
});
}
});
};
};
urls.forEach(function (url, index) {
http.get(url, handlePage(index));
});