Created
September 1, 2012 20:18
-
-
Save daithiw44/3586037 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.
This file contains hidden or 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
/* | |
* 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