Skip to content

Instantly share code, notes, and snippets.

@fastdivision fastdivision/tweets.js
Last active Dec 27, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.