Skip to content

Instantly share code, notes, and snippets.

@simonw
Created May 12, 2010 23:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/399276 to your computer and use it in GitHub Desktop.
Save simonw/399276 to your computer and use it in GitHub Desktop.
/* 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