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

404 error with https.get call to treehouse api

I have been trying to connect to the Treehouse API for Andrew's node,js basics course. Whenver I try to connect it doesn't allow me and says 404.

I have changed the request to https instead of http.

Here is my code:

var https = require("https");

var username = "ethanmoffat6";


function printMessage(username, badgeCount, totalPoints) {
  var message = username + " has " + badgeCount + " total badge(s) and " + totalPoints + " JavaScript points!";
  console.log(message);
}
try {
    var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
    console.error(response.statusCode);
    request.on("data", function(chunk){
      console.log("BODY: " + chunk);  
    });
  });
} 
catch(e) {
  console.log('Catched error: ' + e.message);
}

Let me know any issues please :)

Also, I used try and catch instead of request.on shown in the video.

I have updated my code

//Problem: we need a simple way to look at a user's badge count and javascript points 
//Solution: Use Node.js to connect to Treehouse's API to get profile information to print out

var https = require("https");
var username = "ethanmoffat6";

function printMessage(username, badgeCount, points) {
    var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript";
    console.log(message);
}

//Connect to the API URL (https://teamtreehouse.com/username.json)
var request = https.get("https://www.teamtreehouse.com/" + username + ".json", function(response){
    var body = "";
    //Read the Data
    response.on('data', function (chunk){
        body += chunk;
    });
    response.on('end', function(){
        console.log(body);
        console.log(typeof body);
    });
  //Parse the data 
  //Print the data
  });

request.on("error", function(error){
    console.error(error.message);
});

1 Answer

Issue has so far been resolved. Changed back to request.on and did https://www.domain.com/username.json/ for the request.