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 2017 Building a Command Line Application Parsing JSON

Tim Blokzyl
Tim Blokzyl
145 Points

Link to API is returning 404, has its location changed?

The link to the API included in the workspace is returning a 404. Has its location for this course been changed?

3 Answers

(*SOLVED*) I seem to be getting a 404 error as well.

// 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
const https = require('https');
const username = "shaunhoyes";

// Function to print message to console
function printMessage(username, badgeCount, points) {
  const message = `${username} has ${badgeCount} total badge(s) and ${points} points in JavaScript`;
  console.log(message);
}

// Connect to the API URL (https://teamtreehouse.com/shaunhoyes.json)
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
  let body = ""
  // Read the data 
  response.on('data', data => {
    body += data.toString();
  });

  response.on('end', () => {
    // Parse the data
    const profile = JSON.parse(body);
    console.dir(profile);
    // Print the data 

  });

});

For the https.get request, I had to use ?format=json instead of .json:

const request = https.get(`https://teamtreehouse.com/${username}?format=json`, response => {......
Mike Hickman
Mike Hickman
19,817 Points

Hi Tim,

It's really hard to help narrow it down without seeing your code, but if I remember this lesson correctly, double-check that everything is http*s* and not http. Also, If you're making calls to the treehouse API, make sure there isn't a www. as part of the link. Just https://teamtreehouse.com/blahblah

Good luck,

Mike

They have changed the resful api and added more details to the json structure, this will be a good way to navigate correctly in similar syntax to javascript objects to find the appropriate value.

The syntax is still correct once you recieve the body then parse it using let profile = JSON.parse(body) "profile.profile_name" will give access to his username "profile.badges.length" will give access to the total badges contained in the array across all fields "profile.points.JavaScript" will give access to the JS points accumlated

the json tree / model is best viewed using a tree viewer for navigation/traversal and for familiarity use the json viewer awesome chrome extension.