Created
May 12, 2010 23:36
-
-
Save simonw/399276 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* For Node.js - deal with some of the boilerplate involved in HTTP GET requests */ | |
var http = require('http'), | |
urlmod = require('url'); | |
function getResponse(url, callback) { | |
var parsed = urlmod.parse(url); | |
var port = parseInt(parsed.port || '80', 10); | |
var client = http.createClient(port, parsed.hostname); | |
var path = parsed.pathname; | |
if (parsed.search) { | |
path += '?' + parsed.search; | |
} | |
var request = client.request('GET', path, {'host': parsed.hostname}); | |
request.addListener('response', callback); | |
request.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment