Skip to content
This repository
Browse code

article rendering rss or atom in node

  • Loading branch information...
commit 734a9612bf4afa1f1c2404030a0c6877c0dbc4c6 1 parent 42134f3
Jean-Philippe Monette authored February 27, 2013
112  articles/content-syndication-with-node.markdown
Source Rendered
... ...
@@ -0,0 +1,112 @@
  1
+Title: Content Syndication with Node.js
  2
+Author: Jean-Philippe Monette
  3
+Date: Thu Feb 28 2013 23:30:00 GMT-0500 (EST)
  4
+Node: v0.8.15
  5
+
  6
+Web syndication is a must for any Website wishing to share some entries easily to other systems. Better known under their format name like [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) or [Atom](http://en.wikipedia.org/wiki/Atom_(standard)), they can be quite time consuming to generate without a module handling all their formating. Thanks to the power of Node's package manager [NPM](https://npmjs.org/), you can generate yours in no time.
  7
+
  8
+## Installing the feed wrapper
  9
+
  10
+Before we start, head to your project folder and install the latest version of the module [feed](https://github.com/jpmonette/feed):
  11
+
  12
+	$ npm install feed
  13
+
  14
+## Building the feed
  15
+
  16
+First, we need to initialize a new `Feed` object. When you initialize the object, you must provide general information related to your Web syndication feed. 
  17
+
  18
+	// Require module
  19
+	var Feed = require('feed');
  20
+    
  21
+    // Initializing feed object
  22
+	var feed = new Feed({
  23
+	    title:          'My Feed Title',
  24
+	    description:    'This is my personnal feed!',
  25
+	    link:           'http://example.com/',
  26
+	    image:          'http://example.com/logo.png',
  27
+	    copyright:      'Copyright © 2013 John Doe. All rights reserved',
  28
+
  29
+	    author: {
  30
+	        name:       'John Doe',
  31
+	        email:      'john.doe@example.com',
  32
+	        link:       'https://example.com/john-doe'
  33
+	    }
  34
+	});
  35
+
  36
+Second, you might want to identify your feed thematic. Both [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) and [Atom](http://en.wikipedia.org/wiki/Atom_(standard)) formats offer the possibility to identify one or multiple categories. Again, this is super simple to add:
  37
+
  38
+	feed.category('Node.js');
  39
+	feed.category('JavaScript');
  40
+
  41
+Third, every feed require at least one item (better known as an entry). To do so, you have to use the function `item` and provide the proper object. Of course, if you are running a content Website (like a blog!), chances are that you have multiple items. To populate your feed, use a `for` loop as followed:
  42
+
  43
+	for(var key in posts) {
  44
+	    feed.item({
  45
+	        title:          posts[key].title,
  46
+	        link:           posts[key].url,
  47
+	        description:    posts[key].description,
  48
+	        date:           posts[key].date
  49
+	    });
  50
+	}
  51
+
  52
+At this point, everything is ready to generate your [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) or Atom feed. Use the function `render`:
  53
+
  54
+	var output = feed.render();
  55
+
  56
+This is the implicit way of calling the `render` request. By default, it will render a [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) feed. You an also use the explicit way, allowing you to select between [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) or [Atom](http://en.wikipedia.org/wiki/Atom_(standard)):
  57
+
  58
+	// Rendering a RSS 2.0 valid feed
  59
+	feed.render('rss-2.0');
  60
+
  61
+	// Rendering an Atom 1.0 valid feed
  62
+	feed.render('atom-1.0');
  63
+
  64
+Yes, it's that simple!
  65
+
  66
+## Using with Express.js
  67
+
  68
+Using `feed` with [Express.js](http://expressjs.com/) is super easy. Let's say you have an `app.get()` method routing the path `/rss`. To send your feed, render your feed like we mentionned previously. After, send the result by setting the proper `Content-Type` to `text/xml`:
  69
+
  70
+	app.get('/rss', function(req, res) {
  71
+
  72
+	    // Initializing feed object
  73
+		var feed = new Feed({
  74
+		    title:          'My Feed Title',
  75
+		    description:    'This is my personnal feed!',
  76
+		    link:           'http://example.com/',
  77
+		    image:          'http://example.com/logo.png',
  78
+		    copyright:      'Copyright © 2013 John Doe. All rights reserved',
  79
+
  80
+		    author: {
  81
+		        name:       'John Doe',
  82
+		        email:      'john.doe@example.com',
  83
+		        link:       'https://example.com/john-doe'
  84
+		    }
  85
+		});
  86
+
  87
+		// Function requesting the last 5 posts to a database. This is just an
  88
+		// example, use the way you prefer to get your posts.
  89
+		Post.findPosts(function(posts, err) {
  90
+			if(err)
  91
+				res.send('404 Not found', 404);
  92
+			else {
  93
+				for(var key in posts) {
  94
+				    feed.item({
  95
+				        title:          posts[key].title,
  96
+				        link:           posts[key].url,
  97
+				        description:    posts[key].description,
  98
+				        date:           posts[key].date
  99
+				    });
  100
+				}
  101
+				// Setting the appropriate Content-Type
  102
+				res.set('Content-Type', 'text/xml');
  103
+
  104
+				// Sending the feed as a response
  105
+				res.send(feed.render('rss-2.0'));
  106
+			}
  107
+		});
  108
+	});
  109
+
  110
+## Conclusion
  111
+
  112
+There you go, it was super duper easy :)! Now, people can finally read your entries feeded from your Node.js application!
112  articles/rendering-rss-or-atom-in-node.markdown
Source Rendered
... ...
@@ -0,0 +1,112 @@
  1
+Title: Rendering RSS or Atom in Node.js
  2
+Author: Jean-Philippe Monette
  3
+Date: Thu Feb 28 2013 23:30:00 GMT-0500 (EST)
  4
+Node: v0.8.15
  5
+
  6
+Web syndication is a must for any Website wishing to share some entries easily to other systems. Better known under their format name like [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) or [Atom](http://en.wikipedia.org/wiki/Atom_(standard)), they can be quite time consuming to generate without a module handling all their formating. Thanks to the power of Node's package manager [NPM](https://npmjs.org/), you can generate yours in no time.
  7
+
  8
+## Installing the feed wrapper
  9
+
  10
+Before we start, head to your project folder and install the latest version of the module [feed](https://github.com/jpmonette/feed):
  11
+
  12
+	$ npm install feed
  13
+
  14
+## Building the feed
  15
+
  16
+First, we need to initialize a new `Feed` object. When you initialize the object, you must provide general information related to your Web syndication feed. 
  17
+
  18
+	// Require module
  19
+	var Feed = require('feed');
  20
+    
  21
+    // Initializing feed object
  22
+	var feed = new Feed({
  23
+	    title:          'My Feed Title',
  24
+	    description:    'This is my personnal feed!',
  25
+	    link:           'http://example.com/',
  26
+	    image:          'http://example.com/logo.png',
  27
+	    copyright:      'Copyright © 2013 John Doe. All rights reserved',
  28
+
  29
+	    author: {
  30
+	        name:       'John Doe',
  31
+	        email:      'john.doe@example.com',
  32
+	        link:       'https://example.com/john-doe'
  33
+	    }
  34
+	});
  35
+
  36
+Second, you might want to identify your feed thematic. Both [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) and [Atom](http://en.wikipedia.org/wiki/Atom_(standard)) formats offer the possibility to identify one or multiple categories. Again, this is super simple to add:
  37
+
  38
+	feed.category('Node.js');
  39
+	feed.category('JavaScript');
  40
+
  41
+Third, every feed require at least one item (better known as an entry). To do so, you have to use the function `item` and provide the proper object. Of course, if you are running a content Website (like a blog!), chances are that you have multiple items. To populate your feed, use a `for` loop as followed:
  42
+
  43
+	for(var key in posts) {
  44
+	    feed.item({
  45
+	        title:          posts[key].title,
  46
+	        link:           posts[key].url,
  47
+	        description:    posts[key].description,
  48
+	        date:           posts[key].date
  49
+	    });
  50
+	}
  51
+
  52
+At this point, everything is ready to generate your [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) or Atom feed. Use the function `render`:
  53
+
  54
+	var output = feed.render();
  55
+
  56
+This is the implicit way of calling the `render` request. By default, it will render a [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) feed. You an also use the explicit way, allowing you to select between [RSS](http://en.wikipedia.org/wiki/RSS_(file_format)) or [Atom](http://en.wikipedia.org/wiki/Atom_(standard)):
  57
+
  58
+	// Rendering a RSS 2.0 valid feed
  59
+	feed.render('rss-2.0');
  60
+
  61
+	// Rendering an Atom 1.0 valid feed
  62
+	feed.render('atom-1.0');
  63
+
  64
+Yes, it's that simple!
  65
+
  66
+## Using with Express.js
  67
+
  68
+Using `feed` with [Express.js](http://expressjs.com/) is super easy. Let's say you have an `app.get()` method routing the path `/rss`. To send your feed, render your feed like we mentionned previously. After, send the result by setting the proper `Content-Type` to `text/xml`:
  69
+
  70
+	app.get('/rss', function(req, res) {
  71
+
  72
+	    // Initializing feed object
  73
+		var feed = new Feed({
  74
+		    title:          'My Feed Title',
  75
+		    description:    'This is my personnal feed!',
  76
+		    link:           'http://example.com/',
  77
+		    image:          'http://example.com/logo.png',
  78
+		    copyright:      'Copyright © 2013 John Doe. All rights reserved',
  79
+
  80
+		    author: {
  81
+		        name:       'John Doe',
  82
+		        email:      'john.doe@example.com',
  83
+		        link:       'https://example.com/john-doe'
  84
+		    }
  85
+		});
  86
+
  87
+		// Function requesting the last 5 posts to a database. This is just an
  88
+		// example, use the way you prefer to get your posts.
  89
+		Post.findPosts(function(posts, err) {
  90
+			if(err)
  91
+				res.send('404 Not found', 404);
  92
+			else {
  93
+				for(var key in posts) {
  94
+				    feed.item({
  95
+				        title:          posts[key].title,
  96
+				        link:           posts[key].url,
  97
+				        description:    posts[key].description,
  98
+				        date:           posts[key].date
  99
+				    });
  100
+				}
  101
+				// Setting the appropriate Content-Type
  102
+				res.set('Content-Type', 'text/xml');
  103
+
  104
+				// Sending the feed as a response
  105
+				res.send(feed.render('rss-2.0'));
  106
+			}
  107
+		});
  108
+	});
  109
+
  110
+## Conclusion
  111
+
  112
+There you go, it was super duper easy :)! Now, people can finally read your entries feeded from your Node.js application!
5  authors/Jean-Philippe Monette.markdown
Source Rendered
... ...
@@ -0,0 +1,5 @@
  1
+Github:   jpmonette
  2
+Email:    contact@jpmonette.net
  3
+Homepage: http://blogue.jpmonette.net/
  4
+Twitter:  jpmonette
  5
+Location: Montréal, Québec, Canada

0 notes on commit 734a961

Please sign in to comment.
Something went wrong with that request. Please try again.