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

Christopher Parke
Christopher Parke
21,978 Points

Don't think this works anymore.

I just get this with the exact same code as the video.

treehouse:~/workspace$ node app.js
https.js:193
throw new Error('Unable to determine the domain name');
^

Error: Unable to determine the domain name
at Object.exports.request (https.js:193:13)
at Object.exports.get (https.js:203:21)
at Object.<anonymous> (/home/treehouse/workspace/app.js:13:20)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)

3 Answers

Martin Balon
Martin Balon
43,651 Points

Try changing all http to https in your code. Also in variable request delete "username.json" after forward slash and add https://. The code should look like this:

var https = require("https"); var username = "martinbalon";

function printMessage(username, badgeCount, points){ var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript."; console.log(message); } var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response){ console.log(response.statusCode);
});

request.on("error", function(error){ console.error(error.message); });

yes. It looks like this code does not work and it looks like there is a logical reason that is not accounted for in this course. It is because the string is trying to be parsed into an object (url.parse) and THAT is what is erroring and throwing a chain of events that hits before the https error handling is ran (and that part is a bit messed up here, as well. The url must have been changed to https since this was recorded.) Here looks to be an explanation as to why and a solution outside the scope of this course. http://stackoverflow.com/questions/34933229/why-is-this-basic-node-js-error-handling-not-working.

It looks like this segment needs to be rerecorded or at least some teacher's notes talking about this. It is a problem, IMO.

Ulan Assanoff
Ulan Assanoff
9,922 Points

pls check the teacher's notes which says:

Since Node.js 5 onwards URL parsing errors have to be handled outside of the error event now. Parsing errors can be caught by using a try catch block like so:

try { var request = https.get("teamtreehouse.com/" + username + ".json", function(response){ console.log(response.statusCode); // Read the data // Parse the data // Print the data });

request.on("error", function(e){ console.error(e.message); }); } catch (e) { console.error(e.message); }

Christopher Parke
Christopher Parke
21,978 Points

```var http = require("http");

var username = "chalkers";

function printMessage(username, badgeCount, points) { var message = username + " has " + badgeCount + " total badges and " + points + " points in Javascript"; console.log(message); } //Connect to the API URL (https://teamtreehouse.com/username.json) var request = http.get("teamtreehouse.com/username.json" + username + ".json", function(response) { console.log(response.statusCode);
//Read the data //Parse the data //Print the data

});

request.on("error", function(error) { console.error(error.message);

});```

Christopher Parke
Christopher Parke
21,978 Points

this is my code. the backticks dont seem to work.