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 Parsing and Status Code Errors

ryanjones6
ryanjones6
13,797 Points

Solution: statusCode not displaying short description

If the code below isn't displaying the short description of the status code.

http.STATUS_CODES[response.statusCode]

Then in the top of the document, add a new variable called http that requires "http".

var http = require("http");

5 Answers

Steven Parker
Steven Parker
229,644 Points

You didn't say what your issue is, but perhaps it is due to your second code fragment.

:point_right: Here's a corrected version of that code fragment:

var http = require("http");  // not "requires"
Patryk Bernasiewicz
PLUS
Patryk Bernasiewicz
Courses Plus Student 10,281 Points

In the video, we're trying to display the status description. As of September 2016, the solution shown on the video doesn't work anymore.

The old way to view the status description, shown in the video is this:

http.STATUS_CODES[response.statusCode]

And now it should just look like this:

response.statusMessage

as it's the proper way to achieve what we're trying to in the video, with the newest version of Node.

Maria Victoria Reyes
Maria Victoria Reyes
17,731 Points

Since we are now using "https" to get into treehouse's profile, removing the http.RESPONSE_CODES corrected the very long console error printout I was seeing when I used a wrong version of my username.

/home/treehouse/workspace/app.js:36                                                                
      printError({message :"There was an error getting the profile for " + username + ". (" + http.
STATUS_CODES[response.statusCode] + ")"});                                                         
                                                                                              ^    

ReferenceError: http is not defined                                                                
    at IncomingMessage.<anonymous> (/home/treehouse/workspace/app.js:36:95)                        
    at emitNone (events.js:91:20)                                                                  
    at IncomingMessage.emit (events.js:185:7)                                                      
    at endReadableNT (_stream_readable.js:934:12)                                                  
    at _combinedTickCallback (internal/process/next_tick.js:74:11)                                 
    at process._tickCallback (internal/process/next_tick.js:98:9)   

I think it is because there is no http.RESPONSE_CODES in node.js documentation for using https. Hope this helps. The bottom code made me print out the shorter version of the error with status code.

printError({message :"There was an error getting the profile for " + username + ". (" + response.statusCode + ")"})
;

^^^ Much thanks to Patryk for solving this issue (for me too), but I used response.statusCode (not response.statusMessage) and it worked. Is there a difference?

Thanks!

jennyhan
jennyhan
36,121 Points

Why can't we use response.statusText in Node.js?