0

I have a cofeescript function that I'm trying to write, and I don't know how to get the data to return from the function.

What I want to do is define this function to get a web page for me, assemble the chunks of data, and then return the whole page. For some reason though my return value to console.log is not the page what looks like maybe the response object or request object itself. Below is the code.

    options =
        host:   "www.urbandictionary.com"
        path:   "/define.php?term=Bill"
        method: 'Get'
        port:   80

    getpage = ->
        http.get options, (res) ->
            data = []
            res.on 'data', (chunk) ->
                data.push chunk
                return
            res.on 'end', ->
                data.join ''

    console.log getpage()

In the console I get this response:

ClientRequest {
  domain: null,
  _events: 
   { response: { [Function: g] listener: [Function] },
     socket: { [Function: g] listener: [Function] } },
  _eventsCount: 2,
  _maxListeners: undefined,
  output: [ 'GET /define.php?term=Bill HTTP/1.1\r\nHost: www.urbandictionary.com\r\nConnection: close\r\n\r\n' ],
  outputEncodings: [ 'binary' ],
  outputCallbacks: [ [Function: finish] ],
  outputSize: 88,
  writable: true,
  _last: true,
  chunkedEncoding: false,
  shouldKeepAlive: false,
  useChunkedEncodingByDefault: false,
  sendDate: false,
  _removedHeader: {},
  _contentLength: 0,
  _hasBody: true,
  _trailer: '',
  finished: true,
  _headerSent: true,
  socket: null,
  connection: null,
  _header: 'GET /define.php?term=Bill HTTP/1.1\r\nHost: www.urbandictionary.com\r\nConnection: close\r\n\r\n',
  _headers: { host: 'www.urbandictionary.com' },
  _headerNames: { host: 'Host' },
  _onPendingData: null,
  agent: 
   Agent {
     domain: null,
     _events: { free: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     defaultPort: 80,
     protocol: 'http:',
     options: { path: null },
     requests: {},
     sockets: { 'www.urbandictionary.com:80:': [Object] },
     freeSockets: {},
     keepAliveMsecs: 1000,
     keepAlive: false,
     maxSockets: Infinity,
     maxFreeSockets: 256 },
  socketPath: undefined,
  method: 'GET',
  path: '/define.php?term=Bill' }

If you want to suggest an alternative to 'http', just don't suggest node-jsdom because it dependancies installing in Windows that I don't want to deal with.

Thanks! Bill

Bill Hurt
  • 749
  • 1
  • 8
  • 26
  • This is an asynchronous call. The function returns before the call completes. You need to react to the result in the `onEnd` callback (directly or via something like a Promise). – Thilo Dec 08 '16 at 22:56
  • @Thilo I see what you mean. I'll try the method at the linked answer. – Bill Hurt Dec 08 '16 at 22:58
  • @Thilo I'm fine to mark this as a duplicate if you like. The linked answer is what I need. I'm new to javascript and didn't understand that this was async. Is there a synchronous alternative I can use? This isn't running browser side so I'm not really worried about tying up a thread for a while. – Bill Hurt Dec 08 '16 at 23:11
  • I don't think so. NodeJS is all about avoiding blocking I/O as well, so you all the libraries you will find will be asynchronous. That bit of the learning curve you will have to climb. – Thilo Dec 08 '16 at 23:32

0 Answers0