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

Am I right about what this code is saying?

I passed the challenge, but I want to make sure I know what this code is really doing/saying. So I am going to translate it to English here. If you can correct me if I am wrong, or improve my translation, please do so.

//Require the http API and store it in http.<br>
var http = require("http");

//Get info from given URL, pass it into the function as response, and log the status code
//that is found inside of the info. Lastly, store the info inside request.
var request = http.get("http://teamtreehouse.com/chalkers.json", function(response){
  console.log(response.statusCode);
});

//In the case of (.on) an error in request, pass the error through the function as error,
//and log the error to the console.
request.on('error', function(error){
  console.error(error.message);
});

//Fixed Code Presentation

2 Answers

I would just modify this:

Get info from given URL, pass it into the function as response, and log the status code that is found inside of the info. Lastly, store the info inside request.

To:

Get info from the given URL. On the response event, pass the response to an anonymous function and log out the HTTP status code.

The main clarification being that the status code isn't found within the JSON info received in the response. It just indicates whether the HTTP request was successful.

It's actually 'send a GET HTTP request to the given URL', then on the response event, run the callback function with the resulting object as the response parameter.

The callback function logs the statusCode property to the console.

The request object is assigned to the request variable. It is separate from whatever the response is, and has its own properties and methods.

And the last part would be 'on an error event'.