Making Code Stick: Day 4

Trying to get two today, blogging as I go.

Lesson 7: HTTP Client

Proud to say I only made one mistake in coding this, using import instead of require. Little brain fart. The main handwritten note was about the fact that this seemed to use nested callbacks, which it did.

My solution:

What I found interesting was the solution they show when you’re done is more efficient in a way.

Thought about it for a moment and realized that the data string was going to be passed as the argument to the callback function. So if you just use console.log as the callback, the data string will be passed directly to it without the intermediate handling I put in. They also added error handling, which I leave out because this is quickie ‘pass the exam’ code and not strictly needed. But the habit of putting in error handlers is a good habit to develop.

Lesson 8: HTTP Collect

Required us to install a package to the project dir (npm install [package] instead of npm install -g [package]). It suggested two, of which I chose the first, though it suggested there are more.

The small stupid mistake was adding () onto string.length. That’s a property, not a method, and therefore doesn’t need the parens.

The odd behavior I saw was that the response.setEncoding didn’t work in this example. The response.pipe apparently passes in the raw buffer, whether or not you set an encoding, so the final string needed to be converted with its .toString() method. Interestingly, this time my code was within a few characters of matching the suggested solution.

My Solution:

Leave a Reply

Your email address will not be published. Required fields are marked *