JavaScript Node.js Basics Building a Command Line Application Making a GET Request with https

Wayne Hite
PRO
Wayne Hite
Pro Student 3,002 Points

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

});

1 Answer

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,335 Points

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.