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

APIs

benjaminmosery
benjaminmosery
6,346 Points

Node.js Basics "Making A Get Request": I am getting a "404" statusCode when I request the statusCode, instead of "200"?

Here is my current 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 //Require https module

const https = require(https); const username = "Ben"; //VARIABLE FOR USERNAME

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

//REQUEST TREEHOUSE WEBSITE

//CALL RESPONSE IN DIRECTORY USING.DIR
console.log(response.statusCode);

//Read the Data in //Parse the Json Data (as its in the string) //Print the Data } );

Why am I getting "404" instead of "200", as necessary, when I call "node app.js" in the Console?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! There are a couple of things going on here. First, the API is located at teamtreehouse.com instead of treehouse.com. You've set the username to "Ben". The problem with that is that there is no one on Treehouse with the username "Ben" and nothing else. Take a look at https://teamtreehouse.com/Ben.json. You should get a "Not found" response which is the 404.

My guess here is that you meant to use your username which is "benjaminmosery". This would make the proper URL https://teamtreehouse.com/benjaminmosery.json

Hope this helps! :sparkles:

benjaminmosery
benjaminmosery
6,346 Points

Jennifer,

Thanks alot for all your help, your suggestions led me to getting the correct statusCode! I'll keep in mind your suggestions for the future.

All the best,

Ben