Skip to content

Instantly share code, notes, and snippets.

@nessthehero
Created July 17, 2012 16:32
Show Gist options
  • Save nessthehero/3130477 to your computer and use it in GitHub Desktop.
Save nessthehero/3130477 to your computer and use it in GitHub Desktop.
Node Cacher
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, https = require('https')
, config = require('./feeds')
, cache = require('cache');
var app = module.exports = express.createServer();
var rand = Math.random() * 5 + 1
app.get('/', function(req,res) {
res.send("Test");
});
app.get('/:feed', function(req,res) {
if (typeof config.feeds[req.params.feed] == "object") {
var f = config.feeds[req.params.feed];
var data = "";
console.log(f.uri);
var htpc = https.get({
'host': f.uri,
'port': 443
}, function(res) {
res.setEncoding('utf8');
console.log("in request");
res.on('data',function(chunk){
console.log(chunk.length);
data += chunk;
console.log(data.length)
});
res.on('end', function() {
if (f.ttl != 0 && f.ttl != undefined && f.ttl != null) {
if (data != '') {
console.log(data);
}
}
})
});
htpc.on('error', function(e) {
console.log('problem: '+e.message+" "+f.uri)
for (var i in e) {
console.log(i + ": " + e[i]);
}
});
htpc.end();
}
res.send("Hello!! " + config.feeds[req.params.feed]);
});
app.listen(3000, function(){
console.log("Express server listening on port 3000");
});
// Cacher configuration
exports.feeds = {
"twitter": {
"uri" : "api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=nessthehero&count=1",
// "uri": "www.google.com",
"ttl" : 30
},
"youtube": {
"uri" : "gdata.youtube.com/feeds/api/users/nessthehero/favorites",
// "uri": "www.google.com",
"ttl" : 30
}
}
@sente
Copy link

sente commented Aug 4, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment