Skip to content

Instantly share code, notes, and snippets.

@fastdivision
Last active December 27, 2015 03:09
Show Gist options
  • Save fastdivision/7257449 to your computer and use it in GitHub Desktop.
Save fastdivision/7257449 to your computer and use it in GitHub Desktop.
NodeJS Twitter API v1.1 Proxy
var util = require('util'),
express = require('express'),
twitter = require('mtwitter'),
nconf = require('nconf'),
cache = require('memory-cache');
var port = process.env.PORT || 5000
app = express();
nconf.argv().env().file({ file: 'config.json' });
var twit = new twitter({
consumer_key: process.env.CONSUMER_KEY || nconf.get('CONSUMER_KEY'),
consumer_secret: process.env.CONSUMER_SECRET || nconf.get('CONSUMER_SECRET'),
application_only: true
});
app.get('/tweets', function(req, res) {
if(cache.get('tweets')) {
res.jsonp(cache.get('tweets'));
} else {
twit.get('/favorites/list', { screen_name: 'divshot', count: 50 }, function(err, data) {
if(err) {
console.log(err.toString());
res.send('An error occurred. Please try again later.', 400);
} else {
// Cache for 1 hour
cache.put('tweets', data, 3600000);
res.jsonp(cache.get('tweets'));
}
});
}
});
app.listen(port);
console.log('Started server on: ' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment