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 Making a GET Request with http

Jack Blankenship
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jack Blankenship
Full Stack JavaScript Techdegree Graduate 39,036 Points

getaddrinfo ENOTFOUND on making a GET Request with http in Treehouse Workspaces

I am getting a ENOTFOUND error with the following code. I have tried two different http addresses thinking that Treehouse may be offline ATM.

//console.log("Hello World");
// Treehouse simple process to look at user badge count and JavaScript points
//
var http = require("http");
var username = "chalkers"; 
var httpAddress = "http://teamtreehouse.com/" + username + ".json";

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 @ http://teamtreehouse.com/username.json
var realmStatus =  "/api/wow/realm/status";
var httpHost = "http://us.battle.net";
httpAddress = httpHost + realmStatus;
var request = http.get(httpAddress, function(response) {
    console.log("processing \n");
    console.log(response)
    //Read the data
    //Parse the data
    //Print the data out
});

//handle http errors
request.on("error", function(error){
    console.log("Error processing");
    console.error(error.message);
});

console.log(httpAddress);

From the console.

treehouse:~/workspace$ node app.js                                                    
http://us.battle.net/api/wow/realm/status                                             
Error processing                                                                      
getaddrinfo ENOTFOUND 

I copied and pasted the address directly into a web browser and I got the expected return. Is there an error in my code?

2 Answers

Hey Jack,

So I'm actually encountering the same error in Workspaces, but once I copy/paste everything into my text editor and run node app.js in my shell, it works as expected.

This leads me to believe that it's a Workspaces bug. I'll shoot over a bug report so they can check this out.

Best,

Chris

I tried this within My own console in Ubuntu using NodeJS command line after cleaning up the code and it resulted in a JSON string returning the following:

joel@joel-U80A:~/workspace/odot$ nodejs nodetest.js
http://us.battle.net/api/wow/realm/status
http://us.battle.net/api/wow/realm/status
processing 

{ _readableState: 
   { highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: false,
     ended: false,
     endEmitted: false,
     reading: false,
     calledRead: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     objectMode: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: { end: [Function: responseOnEnd] },
  _maxListeners: 10,
  socket: 
   { _connecting: false,
     _handle: 
      { fd: 10,
        writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread],
        reading: true },
     _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        free: [Function],
        close: [Object],
        agentRemove: [Function],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: false,
     onend: [Function: socketOnEnd],
     destroyed: false,
     errorEmitted: false,
     bytesRead: 8519,
     _bytesDispatched: 83,
     _pendingData: null,
     _pendingEncoding: '',
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _headerSent: true,
        _header: 'GET /api/wow/realm/status HTTP/1.1\r\nHost: us.battle.net\r\nConnection: keep-alive\r\n\r\n',
        _hasBody: true,
        _trailer: '',
        finished: true,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        agent: [Object],
        socketPath: undefined,
        method: 'GET',
        path: '/api/wow/realm/status',
        _headers: [Object],
        _headerNames: [Object],
        parser: [Object],
        res: [Circular] },
     ondata: [Function: socketOnData] },
  connection: 
   { _connecting: false,
     _handle: 
      { fd: 10,
        writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread],
        reading: true },
     _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        free: [Function],
        close: [Object],
        agentRemove: [Function],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: false,
     onend: [Function: socketOnEnd],
     destroyed: false,
     errorEmitted: false,
     bytesRead: 8519,
     _bytesDispatched: 83,
     _pendingData: null,
     _pendingEncoding: '',
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _headerSent: true,
        _header: 'GET /api/wow/realm/status HTTP/1.1\r\nHost: us.battle.net\r\nConnection: keep-alive\r\n\r\n',
        _hasBody: true,
        _trailer: '',
        finished: true,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        agent: [Object],
        socketPath: undefined,
        method: 'GET',
        path: '/api/wow/realm/status',
        _headers: [Object],
        _headerNames: [Object],
        parser: [Object],
        res: [Circular] },
     ondata: [Function: socketOnData] },
  httpVersion: '1.1',
  complete: false,
  headers: 
   { date: 'Wed, 18 Feb 2015 10:59:15 GMT',
     server: 'Apache',
     'x-frame-options': 'SAMEORIGIN',
     'last-modified': 'Wed, 18 Feb 2015 10:59:09 GMT',
     'content-language': 'en-US',
     vary: 'Accept-Encoding',
     'keep-alive': 'timeout=10, max=4000',
     connection: 'Keep-Alive',
     'transfer-encoding': 'chunked',
     'content-type': 'application/json;charset=UTF-8' },
  trailers: {},
  _pendings: [],
  _pendingIndex: 0,
  url: '',
  method: null,
  statusCode: 200,
  client: 
   { _connecting: false,
     _handle: 
      { fd: 10,
        writeQueueSize: 0,
        owner: [Circular],
        onread: [Function: onread],
        reading: true },
     _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: true,
     domain: null,
     _events: 
      { end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        free: [Function],
        close: [Object],
        agentRemove: [Function],
        drain: [Function: ondrain],
        error: [Function: socketErrorListener] },
     _maxListeners: 10,
     _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     allowHalfOpen: false,
     onend: [Function: socketOnEnd],
     destroyed: false,
     errorEmitted: false,
     bytesRead: 8519,
     _bytesDispatched: 83,
     _pendingData: null,
     _pendingEncoding: '',
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Circular],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     _httpMessage: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: true,
        useChunkedEncodingByDefault: false,
        sendDate: false,
        _headerSent: true,
        _header: 'GET /api/wow/realm/status HTTP/1.1\r\nHost: us.battle.net\r\nConnection: keep-alive\r\n\r\n',
        _hasBody: true,
        _trailer: '',
        finished: true,
        _hangupClose: false,
        socket: [Circular],
        connection: [Circular],
        agent: [Object],
        socketPath: undefined,
        method: 'GET',
        path: '/api/wow/realm/status',
        _headers: [Object],
        _headerNames: [Object],
        parser: [Object],
        res: [Circular] },
     ondata: [Function: socketOnData] },
  _consuming: false,
  _dumped: false,
  httpVersionMajor: 1,
  httpVersionMinor: 1,
  upgrade: false,
  req: 
   { domain: null,
     _events: { error: [Function] },
     _maxListeners: 10,
     output: [],
     outputEncodings: [],
     writable: true,
     _last: true,
     chunkedEncoding: false,
     shouldKeepAlive: true,
     useChunkedEncodingByDefault: false,
     sendDate: false,
     _headerSent: true,
     _header: 'GET /api/wow/realm/status HTTP/1.1\r\nHost: us.battle.net\r\nConnection: keep-alive\r\n\r\n',
     _hasBody: true,
     _trailer: '',
     finished: true,
     _hangupClose: false,
     socket: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        onend: [Function: socketOnEnd],
        destroyed: false,
        errorEmitted: false,
        bytesRead: 8519,
        _bytesDispatched: 83,
        _pendingData: null,
        _pendingEncoding: '',
        parser: [Object],
        _httpMessage: [Circular],
        ondata: [Function: socketOnData] },
     connection: 
      { _connecting: false,
        _handle: [Object],
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: true,
        allowHalfOpen: false,
        onend: [Function: socketOnEnd],
        destroyed: false,
        errorEmitted: false,
        bytesRead: 8519,
        _bytesDispatched: 83,
        _pendingData: null,
        _pendingEncoding: '',
        parser: [Object],
        _httpMessage: [Circular],
        ondata: [Function: socketOnData] },
     agent: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        options: {},
        requests: {},
        sockets: [Object],
        maxSockets: 5,
        createConnection: [Function] },
     socketPath: undefined,
     method: 'GET',
     path: '/api/wow/realm/status',
     _headers: { host: 'us.battle.net' },
     _headerNames: { host: 'Host' },
     parser: 
      { _headers: [],
        _url: '',
        onHeaders: [Function: parserOnHeaders],
        onHeadersComplete: [Function: parserOnHeadersComplete],
        onBody: [Function: parserOnBody],
        onMessageComplete: [Function: parserOnMessageComplete],
        socket: [Object],
        incoming: [Circular],
        maxHeaderPairs: 2000,
        onIncoming: [Function: parserOnIncomingClient] },
     res: [Circular] } }

Looks like your code works.

Was this the return you were expecting? I don't know what pulls from that URL, so I'm guessing it's something to do with WoW servers?

Hope this helps.