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