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 trialMike Ulvila
16,268 PointsSome 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);
4 Answers
Mike Ulvila
16,268 PointsSorry Michael, I just copied and pasted from my workspace. Don't know how to make it look like the link you gave.
lfkwtz
12,295 PointsClick the Markdown Cheatsheet link underneath the textbox for an explanation on how to format your code.
roeroe
11,826 PointsHey 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.
shemaiah cox
5,722 PointsBrill!
Brian Stumbaugh
541 PointsMike....I don't think programming is your cup of tea. Consider going back to playing your geetar. Haha! See you tomorrow.
Mike Ulvila
16,268 PointsI really want to add this as best answer haha
lfkwtz
12,295 Pointslfkwtz
12,295 PointsYour 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