Created
September 15, 2012 17:04
-
-
Save davybrion/3728853 to your computer and use it in GitHub Desktop.
code snippets for "Displaying Feed Items On A Web Page: My Solution" post
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
var express = require('express'), | |
app = module.exports = express.createServer(), | |
NodePie = require('nodepie'), | |
request = require('request'), | |
recentFeedItems = null; | |
app.dynamicHelpers({ | |
getRecentFeedItems: function() { | |
return recentFeedItems; | |
} | |
}); | |
// ... some extra configuration of Express that isn't relevant to this post | |
var processFeed = function(callback) { | |
request('http://feeds.feedburner.com/davybrion', function(err, response, body) { | |
if (!err && response.statusCode == 200) { | |
var feed = new NodePie(body); | |
feed.init(); | |
recentFeedItems = feed.getItems(0, 5); | |
if (callback) callback(); | |
}; | |
}); | |
}; | |
setInterval(processFeed, 1800000); // process feed items every 30 minutes | |
processFeed(function() { | |
app.listen(3000); | |
console.log('Express started 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
<ul> | |
<% getRecentFeedItems.forEach(function(item) { %> | |
<li><time class="date"><%= item.getDate().getDate() + '/' + (item.getDate().getMonth() + 1) %></time><a href="<%= item.getPermalink() %>"><%= item.getTitle() %></a></li> | |
<% }); %> | |
</ul> |
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
app.dynamicHelpers({ | |
getRecentFeedItems: function() { | |
return recentFeedItems; | |
} | |
}); |
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
var processFeed = function(callback) { | |
request('http://feeds.feedburner.com/davybrion', function(err, response, body) { | |
if (!err && response.statusCode == 200) { | |
var feed = new NodePie(body); | |
feed.init(); | |
recentFeedItems = feed.getItems(0, 5); | |
if (callback) callback(); | |
}; | |
}); | |
}; |
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
setInterval(processFeed, 1800000); // process feed items every 30 minutes | |
processFeed(function() { | |
app.listen(3000); | |
console.log('Express started on port 3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment