Create a gist now

Instantly share code, notes, and snippets.

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