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 (2014) Building a Command Line Application Handling Errors in Node

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Trying to make an error event handler on node.js

Hi all,

So I'm a little stumped as to why this shouldn't be working.

I'm pretty sure I'm using the right methods variable request and method in on and the correct syntax for a callback function. What am I missing? :)

Thanks.

app.js
var http = require("http");

http.get("http://teamtreehouse.com/chalkers.json", function(response){
  console.log(response.statusCode);
});

var request = http.get;

request.on("error", function(error) {

});

2 Answers

Henrique Zafniksi
Henrique Zafniksi
8,560 Points

Evaluate the request variable to the method get, on the http variable. Making a later reference will create another instance instead of assigning to it.

var request = http.get("http://teamtreehouse.com/chalkers.json", function(response){
     console.log(response.statusCode);
});

In case of errors, try this, as it will console the specific error to you:

request.on("error", function(error){
  console.error(error.message);
});
Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Henquitw,.

Well I'm still stumped, and this the first time i've had in 5 days to come back to it.

This ought to be a simple task....

Aha, I've since worked it out.

We were supposed to add var request = to the third line of my code. As you said. It took a while for me to work it all out but the ligthbulb in my brain seems to be switched on now.