Created
July 17, 2012 16:32
-
-
Save nessthehero/3130477 to your computer and use it in GitHub Desktop.
Node Cacher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out Hyve -- https://github.com/sente/hyve/blob/develop/demos/node_demo.js
Hyve is part of Tawlk -- http://tawlk.com