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

http.STATUS_CODES no longer applicable

Now that we are accessing with an 'https' protocol, there doesn't appear to be an equivalent STATUS_CODES object. Is there an alternative method or is this practice not applicable for https?

I can't find the docs for it myself :)

Good question;)

7 Answers

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

you can still use http.STATUS_CODES even though the request is made over https, you'd just require both :)

[nathwill@wyrd ~]$ node
> var http = require('http');
undefined
> var https = require('https');
undefined
> console.dir(http.STATUS_CODES)
{ '100': 'Continue',
  '101': 'Switching Protocols',
  '102': 'Processing',
  '200': 'OK',
  '201': 'Created',
  '202': 'Accepted',
  '203': 'Non-Authoritative Information',
  '204': 'No Content',
  '205': 'Reset Content',
  '206': 'Partial Content',
  '207': 'Multi-Status',
  '300': 'Multiple Choices',
  '301': 'Moved Permanently',
  '302': 'Moved Temporarily',
  '303': 'See Other',
  '304': 'Not Modified',
  '305': 'Use Proxy',
  '307': 'Temporary Redirect',
  '400': 'Bad Request',
  '401': 'Unauthorized',
  '402': 'Payment Required',
  '403': 'Forbidden',
  '404': 'Not Found',
  '405': 'Method Not Allowed',
  '406': 'Not Acceptable',
  '407': 'Proxy Authentication Required',
  '408': 'Request Time-out',
  '409': 'Conflict',
  '410': 'Gone',
  '411': 'Length Required',
  '412': 'Precondition Failed',
  '413': 'Request Entity Too Large',
  '414': 'Request-URI Too Large',
  '415': 'Unsupported Media Type',
  '416': 'Requested Range Not Satisfiable',
  '417': 'Expectation Failed',
  '418': 'I\'m a teapot',
  '422': 'Unprocessable Entity',
  '423': 'Locked',
  '424': 'Failed Dependency',
  '425': 'Unordered Collection',
  '426': 'Upgrade Required',
  '428': 'Precondition Required',
  '429': 'Too Many Requests',
  '431': 'Request Header Fields Too Large',
  '500': 'Internal Server Error',
  '501': 'Not Implemented',
  '502': 'Bad Gateway',
  '503': 'Service Unavailable',
  '504': 'Gateway Time-out',
  '505': 'HTTP Version Not Supported',
  '506': 'Variant Also Negotiates',
  '507': 'Insufficient Storage',
  '509': 'Bandwidth Limit Exceeded',
  '510': 'Not Extended',
  '511': 'Network Authentication Required' }
undefined
> https.get('https://teamtreehouse.com/chalkers.json', function(response){ console.log(http.STATUS_CODES[response.statusCode]); })
{ domain: null,
  _events: 
   { response: { [Function: g] listener: [Function] },
     socket: { [Function: g] listener: [Function] } },
  _maxListeners: 10,
  output: [ 'GET /chalkers.json HTTP/1.1\r\nHost: teamtreehouse.com\r\nConnection: keep-alive\r\n\r\n' ],
  outputEncodings: [ undefined ],
  writable: true,
  _last: true,
  chunkedEncoding: false,
  shouldKeepAlive: true,
  useChunkedEncodingByDefault: false,
  sendDate: false,
  _headerSent: true,
  _header: 'GET /chalkers.json HTTP/1.1\r\nHost: teamtreehouse.com\r\nConnection: keep-alive\r\n\r\n',
  _hasBody: true,
  _trailer: '',
  finished: true,
  _hangupClose: false,
  socket: null,
  connection: null,
  agent: 
   { domain: null,
     _events: { free: [Function] },
     _maxListeners: 10,
     options: {},
     requests: {},
     sockets: { 'teamtreehouse.com:443': [Object] },
     maxSockets: 5,
     createConnection: [Function: createConnection] },
  socketPath: undefined,
  method: 'GET',
  path: '/chalkers.json',
  _headers: { host: 'teamtreehouse.com' },
  _headerNames: { host: 'Host' } }
> OK
> 

as a node-n00b, it's super hilarious that node has separate http and https modules at all, i wonder what the reasoning is!

Dylan Slade
Dylan Slade
5,499 Points

Thanks Nathan. I simply required http and my error message displays properly now.

Sebastian Röder
Sebastian Röder
13,878 Points

There is an easier way to get the status message that does not involve the STATUS_CODES object and works for both http and https: the statusMessage property of the response object.

console.log("Error fetching data for user " + username + ": " + response.statusMessage);

The console output looks like this:

$ node app.js 
GET https://teamtreehouse.com/sebroeder42.json: 404
Error fetching data for user sebroeder42: Not Found

(Sorry for the double post, I could not find a way to link to my answer in the other thread).

Thanks for that Nathan.

Got me worked up a bit... And I agree on the separate modules for the http and https requests...

ALDRICH ALLEN Barcenas
ALDRICH ALLEN Barcenas
4,018 Points

I find it also very odd that they don't have the same set of methods or at least extend one over the other (if there is such a thing in node or Javascript).

Awesome. thanks!

Thanks Sebastian. That console.log("Error fetching data for user " + username + ": " + response.statusMessage); line was the solution for me.

Eric Thompson
Eric Thompson
9,858 Points

Adding var http = require("http"); worked for me as well!