Skip to content

Instantly share code, notes, and snippets.

@hisasann
Created February 24, 2012 06:16
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 hisasann/1898324 to your computer and use it in GitHub Desktop.
Save hisasann/1898324 to your computer and use it in GitHub Desktop.
InstagramのtagAPIを使ってJSONを返すnode.js版プロキシー
var url = require('url'),
https = require('https'),
http = require('http'),
express = require('express');
var recent = '';
var app = express.createServer();
app.set('view options', { layout: false });
app.get('/instagram_tag', function(req, res){
var code = req.query.code;
if (code) {
return;
}
var access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
var u = url.parse('https://api.instagram.com/v1/tags/' + req.query.tag + '/media/recent');
var param = "?access_token=" + access_token;
var head = true;
https.get({
host: u.hostname,
port: u.port,
path: u.pathname + param,
method: 'POST'
}, function(data) {
var body = '';
data.on('data', function(chunk) {
if(head){
res.writeHead(200, {'Content-Type': 'text/plain'});
head = false;
}
body += chunk.toString();
});
data.on('end', function() {
recent = JSON.parse(body);
res.write(JSON.stringify(recent));
res.end();
});
}).on('error', function(e) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(e);
res.end();
});
});
app.listen(8126);
var Log = require('log'),
fs = require('fs'),
stream = fs.createWriteStream(__dirname + '/instagram_tag.log'),
log = new Log(Log.INFO, stream);
// catchされなかった例外の処理設定
process.on('uncaughtException', function (err) {
log.info('$: uncaughtException => ' + err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment