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 Making a GET Request with https

When running console.log(response.statusCode); I received 404 not 200 as indicated in the video. My code looks identical

Hello, I ran "node app.js" in terminal and it output 404 instead of 200. My code looks identical to the video. Any suggestions what would cause this and how to correct? Here is my code. Thanks!

// 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

// Require https module const https = require('https'); const username = "wayne";

// 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/waynehite.json) const request = https.get(https://teamtreehouse.com/${username}.json, response => {

console.log(response.statusCode);

// Read the data // Parse the data // Print the data

});

5 Answers

latildarich
latildarich
4,142 Points

Hi, make sure you are using a template literal for your http.get url. I also made this mistake and figured out that is what I had done. If it's not in backticks (template literal), it won't read the username correctly.

For example

const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {

                          console.log(response.statusCode);
                            // Read the data
                            // Parse the data
                            // Print the data
                          });

It's hard to tell with the formatting if below is the same code but it produced a status code of 200.

// Require https module
const https = require('https');
const username = "waynehite";

// 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/waynehite.json)
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
  console.log(response.statusCode);
// Read the data // Parse the data // Print the data
});

Note: I changed the username to "waynehite". "wayne" resulted in a different account but also resulted in a status code of 200.

Check your teamtreehouse profile privacy settings. Mine was set to private so returned a 404. When I ticked option to allow public got the 200 code.

For me, the issue was that I had the username wrong it had to be "waynehite" and not something else to work, then you will get a 200 instead of 404.