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

400 error instead of Error Event

Hi! I'm trying to follow along with the following code:

var http = require("http");
var username = "chalkers"; 

function printMessage(username, badgecount, points){ 
    var message = username + " has " + badgecount + " total badge(s) 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", function(response){ 
    console.log(response.statusCode); 
    //Read the data 
    //Parse the data 
    //Print the data out 
}); 

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

However instead of getting the " throw er; // Unhandled 'error' event" response, I get a status code of 400. I am following along using node version v0.12.2 running locally on a mac. When trying through Workspaces, there is not a problem.

Hi Pete, just letting you know I edited your question to properly format the code block. You need to have an empty line proceeding and following the three backticks.

Also, if you put the name/file extension of the type of code immediately after the opening backticks, on the same line, you will get the syntax highlighting.

In your case, ```js will show the JavaScript syntax highlighting.

2 Answers

Hi.

Had the same problem. The solution is - disable local server you are currently running (WAMP, Denwer, Open Server, XAMPP, etc.) and request will throw an error instead of 400.

This worked thanks !

To answer your question, I think you have to include the protocol in the http.get method's first parameter:

var request = http.get("http://teamtreehouse.com/" +username +".json", function(response){ 

If you don't, it will assume it's a local address.