How can I solve learnyounode http-collect without a third-party library

I am new to node and I am going through nodeschool.io interactive learnyounode tutorials and I have problem with http-collect problem.

Firstly, the actual solution that uses the third-party Buffered List package to solve the problem does not work when I run the learnyounode verify command. I'm not even sure how to test this side of the learnyounode test. How can I do it? Am I assuming some kind of node service?

Secondly, since I really do not understand this, I would really like to try to implement it without a third-party package, but I do not know where to start. I am not looking for a solution for me, but I really appreciate what is needed in the right direction, be it an online tutorial or a specific method in the node documentation that can help.

For reference, this is learnyounode solution which I am not able to

 var http = require('http') var bl = require('bl') http.get(process.argv[2], function (request) { request.pipe(bl(function (err, data) { if (err) return console.error(data) data = data.toString() console.log(data.length) console.log(data) })) }) 

Exit learnyounode verify http-collect

 Verifying "HTTP COLLECT"... ACTUAL: "" EXPECTED: "123" ACTUAL: null EXPECTED: "Flat out like a swag no worries as dry as a big smoke. Lets throw a fruit loop how as stands out like not my bowl of rice. " ACTUAL: null EXPECTED: "" # FAIL Your solution to HTTP COLLECT didn't match the expected output. Try again! 

Update: possible proxy problem ...

Further update: not a problem with the proxy server, but leaving this section here in case the information is useful to someone else

So, I believe that I understood the problem, but still did not understand the solution. I think this is a proxy problem since I am doing this on a production network. I came to this conclusion because I had the same error with the following

 http.get({path:'http://www.google.com/index.html'}, function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); }); 

as soon as I changed the fragment above to the next one, where our company’s proxy server is http://http.proxy.somewhere.com:1234 , I no longer receive an error for this fragment, unfortunately, this is not enough to check learnyounode.

 http.get({ host: 'http.proxy.somewhere.com', path:'http://www.google.com/index.html', port: 1234}, function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); }); 

Buffered List Solution

The problem was that the bl module was not installed locally, I installed it globally initially and it seems to be causing the problem, I don’t know why, if anyone can explain why this could be the problem, please

+1
javascript
source share
4 answers

I apologize for this. Strange, I just finished this exercise. I checked your solution on my computer and it worked. Did you install bl before writing the program? npm install bl

In the case without a third-party package.

-If you use the response.on method - there are several data events, so you have to add them to your callback and save them in a variable. - you will need to use response.setEncoding in the same way as the exercise for httpclient; - use the callback of the response.end method to output

+2
source

Here is my solution if you do not want to use a third party:

 var http = require('http'); var info = []; var url = process.argv[2]; http.get(url, function(res) { res.setEncoding('utf8'); res.on('data', function(data) { info.push(data); }); res.on('error', console.error); res.on('end', function() { console.log(info.join('').length); console.log(info.join('')); }); }); 
+2
source

To answer your question "without a third-party library"

The response object from the http.get callback emits the final event. I use the same technique as the previous exercise to get the data in a string format, and then add it to the variable (starting with an empty line) in each data event, and then output the result at the end of the event.

Cannot use .setEncoding('utf8') . I would really like to see a solution that combines Buffer objects (without a third-party library), then use .toString() at the end, though.

Below is my callback:

 function httpGetCallback(response) { var result = ''; function appendResult(data) { result += data; } function logResult() { console.log(result.length); console.log(result); } response.setEncoding('utf8'); response.on('data', appendResult); response.on('end', logResult); } 

A quick note: I'm curious if the http module has one to ensure the order of the data blocks.

+1
source

I was able to solve this problem by installing the local Buffered List module. I originally installed it all over the world, but for some reason this causes a problem. Still not sure why ...

-one
source

All Articles