HTTP requests with Node.js

I couldn’t find this code anywhere on the internets yesterday, posting here for my own convenience.



 var options = {
   host: 'api.twitter.com',
   port: 80,
   path: '/path',
   method: 'GET',
   encoding: 'utf8'
 };


 http.get({ host: options.host, path: options.path }, function(res2){
	var data = [];
	res2.on('data', function(chunk) { 
		data.push(chunk); 
	});


	res2.on('end', function() { // wait for the request to finish
		res.json(data.join('')); 
	})
});

Advertisements