Skip to content

Instantly share code, notes, and snippets.

@gotomypc
Forked from daithiw44/server.js
Created October 28, 2012 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotomypc/3967462 to your computer and use it in GitHub Desktop.
Save gotomypc/3967462 to your computer and use it in GitHub Desktop.
GET users/profile_image/:screen_name, Nodejs get latest user profile_image and avoid the 302 redirect with request.
/*
* Access the profile image in various sizes for the user with the indicated screen_name.
* Currently this resource does not return JSON (or XML), but instead returns a 302 redirect to the actual image resource.
* Use requests request.head with option followRedirect set to false to get the image file name from the json in the header response.
* example : http://127.0.0.1:4400/?screen_name=daithi44
*/
var http = require('http')
, request = require('request')
, url = require('url');
http.createServer(function(req, res) {
var twitterwho = url.parse(req.url, true).query;
if (twitterwho.hasOwnProperty('screen_name')) {
request.head({uri: 'http://api.twitter.com/1/users/profile_image?screen_name=' + twitterwho.screen_name + '&size=normal', followRedirect: false}, function(error, imageresponse) {
if (!error && imageresponse.statusCode === 302) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(imageresponse.headers.location);
} else {
displayThis();
}
});
} else {
displayThis();
}
function displayThis() {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('something else');
}
}).listen(4444);
console.log('Server running at http://127.0.0.1:4444/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment