yiddish.ninja
Menu
  • About
  • Bedtime Stories
Menu

Making Code Stick: Day 4

Posted on June 12, 2015 by Greg

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:

Learnyounode 7: HTTP Client
JavaScript
1
2
3
4
5
6
7
8
var http = require('http');
 
http.get(process.argv[2],function (response){
  response.setEncoding('utf8');
  response.on("data", function(data){
    console.log(data);
  });
});

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

their solution - excerpt
JavaScript
1
2
  response.on('data', console.log)
  response.on('error', console.error)

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:

HTTP Collect solution
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
var http = require('http');
var bl = require('bl');
 
http.get(process.argv[2],function (response){
  response.pipe(bl(function(err, data){
    if(err) {
     return console.error(err);
     }
    data = data.toString();
    console.log(data.length);
    console.log(data);
  }));
});

Leave a Reply Cancel reply

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

Recent Posts

  • What is Programming?
  • Becoming a Raspberry Pi Certified Educator
  • MOD Pizza’s Cauliflower Crust
  • Keeping my resolutions
  • Pain and Python

Archives

Categories

Disclaimer

The views and opinions expressed on this site are my own and do not represent any organizations, projects. or businesses with which I am involved as an employee, member, participant, or leader.

Recent Comments

  • Ron on Lowe’s: A Case Study in Bad Customer Service
  • Greg on Viewing Someone’s Previous LinkedIn Posts
  • Abhiram Venkitela on Viewing Someone’s Previous LinkedIn Posts
  • Pikachu1902 on Sharing Your Local Minecraft Server Over the Internet
  • John MARTIN on Mags for your Canik TP40

No Warranty

I'm human, I'm imperfect, and I make mistakes. There is no guarantee of accuracy, serviceability, functionality, or applicability of anything I write or link to here. By viewing this site, you agree that you are solely responsible for all outcomes related to that act and release me from any liability.
©2019 yiddish.ninja | WordPress Theme by Superb Themes