Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for RSS feed

An RSS feed of the blog is now available via blog_url/rss.xml .  This
uses the [node-rss](https://github.com/dylang/node-rss) module.  Please
also update you template as it now supports auto discovery of RSS feeds.
  • Loading branch information...
commit 141a6a5abee10f95babe00304d5ea6dab1201b71 1 parent 6d175b7
Guy Halford-Thompson authored
Showing with 57 additions and 5 deletions.
  1. +52 −4 lib/glog.js
  2. +2 −1  package.json
  3. +3 −0  server.js
View
56 lib/glog.js
@@ -4,8 +4,9 @@
var path = require('path'),
fs = require('fs'),
marked = require('marked'),
- jade = require('jade'),
- exec = require('child_process').exec;
+ jade = require('jade'),
+ exec = require('child_process').exec,
+ rss = require('rss');
/*
@@ -152,7 +153,37 @@ Glog.prototype.load_articles = function(options, cb) {
});
}
});
-}
+};
+
+
+/*
+ * Create the RSS feed
+ */
+Glog.prototype.render_rss = function(options, articles, cb) {
+
+ var feed = new rss({
+ title : options.blog_title,
+ feed_url : (options.base_url || '') + '/rss.xml',
+ site_url : (options.base_url || ''),
+ author : options.author
+ }),
+ len = articles.length, i;
+
+ for(i=0;i<len;i++) {
+ feed.item({
+ title : articles[i].title,
+ description : articles[i].body,
+ url : articles[i].url,
+ author : articles[i].author,
+ date : articles[i].date
+ });
+ }
+
+ this.pages['rss'] = feed.xml();
+
+ cb();
+
+};
/*
@@ -235,6 +266,21 @@ Glog.prototype.req_home = function(req, res, next, options) {
/*
+ * Handle a request for the RSS feed
+ */
+Glog.prototype.req_rss = function(req, res, next, options) {
+ console.log('RSS feed requested');
+
+ // Set headers
+ res.setHeader('Content-Type', 'application/rss+xml; charset=utf-8');
+ res.setHeader('Cache-Control', options.cache_time || 'no-cache');
+ res.setHeader('Server', 'Glog (NodeJS)');
+
+ res.end(this.pages['rss']);
+};
+
+
+/*
* Handle a request for an individual article
*/
Glog.prototype.req_article = function(req, res, next, options) {
@@ -280,7 +326,9 @@ Glog.prototype.rebuild = function(cb) {
articles = articles.sort(function(a,b) {
return b.date - a.date;
});
- fn.render_blog(options, articles, cb);
+ fn.render_rss(options, articles, function() {
+ fn.render_blog(options, articles, cb);
+ });
});
});
});
View
3  package.json
@@ -7,7 +7,8 @@
"connect" : ">= 1.8.4",
"marked" : ">= 0.1.4",
"jade" : ">= 0.19.0",
- "mocha": ">= 0.8.2"
+ "mocha": ">= 0.8.2",
+ "rss": ">= 0.0.3"
},
"scripts": {
"test": "make test"
View
3  server.js
@@ -31,6 +31,9 @@ glog.rebuild(function() {
app.get('/:year/:month/:article', function(req, res, next) {
glog.req_article(req, res, next, options)
});
+ app.get('/rss.xml', function(req, res, next) {
+ glog.req_rss(req, res, next, options)
+ });
app.get('/*', function(req, res, next) {
glog.req_home(req, res, next, options)
});
Please sign in to comment.
Something went wrong with that request. Please try again.