Skip to content

Instantly share code, notes, and snippets.

@ivaravko
Forked from bdickason/node-httprequest
Created April 29, 2012 19:46
Show Gist options
  • Save ivaravko/2552922 to your computer and use it in GitHub Desktop.
Save ivaravko/2552922 to your computer and use it in GitHub Desktop.
Simple HTTP Request in NodeJS
getRequest: (callback) ->
options = {
host: config.host or 'yourserver'
port: config.port or 80
path: config.path or '/api/'
method: config.method or 'GET'
tmp = [] # Russ at the NYC NodeJS Meetup said array push is faster
http.request _options, (res) ->
res.setEncoding 'utf8'
res.on 'data', (chunk) ->
tmp.push chunk # Throw the chunk into the array
res.on 'end', (e) ->
body = tmp.join('')
callback body # Just spit back the body, no headers!
.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment