Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics (2014) Building a Command Line Application Handling Parsing and Status Code Errors

Mike Ulvila
Mike Ulvila
16,268 Points

Some serious confusion between require http and https for us beginners. Please consider an update to this tutorial.

you must require both http and https and use them in different parts of the code to make this tutorial work correctly. Here is what I came up with to make the error print like the tutorial.

var https = require('https');
var http = require('http');
var username = 'asdhfjkasdgjkasgjkh';

// print out message
function printMessage(username, badgeCount, points) {
  var message = username + ' has ' + badgeCount + ' total badge(s) and ' + points + ' points in javascript.';
      console.log(message);
}

// print out error message
function printError (error) {
  console.log("error", error);
}
//connect to the api url
var request = https.get('https://teamtreehouse.com/' + username + '.json', function (response) {
  var body = "";
  //read the data
  response.on('data', function (chunk) {
    body += chunk;
  });
  response.on('end', function () {
    if (response.statusCode === 200) {
      try {
        var profile = JSON.parse(body);
        printMessage(username, profile.badges.length, profile.points.JavaScript);
      } catch (error) {
        printError(error);
      } 
    } else {
        printError({message: "there was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] +         ")"});
      }
   });
  //parse the data
  //print the data 
});

// connection error
request.on('error', printError);

Your code isn't formatted very well and it's pretty hard to read.

This post has a great working example :https://teamtreehouse.com/community/ive-got-a-typeerror-instead-of-printing-out-message-to-the-console

4 Answers

Mike Ulvila
Mike Ulvila
16,268 Points

Sorry Michael, I just copied and pasted from my workspace. Don't know how to make it look like the link you gave.

Click the Markdown Cheatsheet link underneath the textbox for an explanation on how to format your code.

roeroe
roeroe
11,826 Points

Hey Mike, node.js has built in modules such as like fs, http, or https. The fs(file system) module is used to read, write, and delete files. The http modules is used to get information from an external source and to create servers. The https module does the same thing expect in the https this us used as a more secure way of protecting users information. You actually don't need to acquire the http module but only the https module since your link has "https://teamtreehouse.com/". I hope I answered your question.

Brian Stumbaugh
Brian Stumbaugh
541 Points

Mike....I don't think programming is your cup of tea. Consider going back to playing your geetar. Haha! See you tomorrow.

Mike Ulvila
Mike Ulvila
16,268 Points

I really want to add this as best answer haha