Add RSS feed to your Node.Js website

Posted by Nishant on 20, Apr 2016

If you are like me then you might be following several interesting websites and smart folks on their blogs. With so much content to read everyday, we start running out of time to consume all this information and it becomes important to know what to omit while deciding what you will be reading. Of course we certainly do not have time to visit all these websites frequently just to know whether there is a new info available, here come all these RSS reader apps which give all this information in one place.

You can see if you have your own blog and do not have RSS feed in your website then you are not thinking about such users and inadvertently sending them off resulting in low traffic to your website.

So If you have a website running on Node.js, and you are looking for a way to add RSS feed endpoint, then this post is for you, it's very easy, keep reading.

Setup

We will quickly build an express.js website without any cruft to add one endpoint which serves RSS feed. Following is the app.js file which has all the code needed to run our website:

const express = require('express');
const Feed = require('feed');

var app = express();

app.get('/feed/rss', function (req, res) {

    // Get the posts from the database or whatever data store you are using, I am simply goin to use in memory array
    var posts = [
        {
            title: "Post 1",
            summary: "Post 1 summary",
            content: "Post 1 content",
            pubdate: new Date(),
            slug: 'post-1'
        },
        {
            title: "Post 2",
            summary: "Post 2 summary",
            content: "Post 2 content",
            pubdate: new Date(),
            slug: "post-2"
        },
        {
            title: "Post 3",
            summary: "Post 3 summary",
            content: "Post 3 content",
            pubdate: new Date(),
            slug: "post-3"
        },
        {
            title: "Post 4",
            summary: "Post 4 summary",
            content: "Post 4 content",
            pubdate: new Date(),
            slug: "post-4"
        }
    ];

    // Instantiate feed object

    var feed = new Feed({
        title: 'Example',
        description: "An interesting site which talks sense",
        link: "http://example.com"
    });

    //add posts

    posts.forEach(function (p) {
        feed.addItem({
            title: p.title,
            link: "http://example.com/" + p.slug,
            description: p.summary,
            date: p.pubdate
        });
    });

    //add categories

    feed.addCategory('Technology');
    feed.addCategory('Programming');
    feed.addCategory('Web Development');

    //render xml in rss or atom format depending upon the url param

    var feedRes = feed.render('rss-2.0');
    
    res.set('Content-Type', 'text/xml');
    res.send(feedRes);
});

app.listen(3000,function(){
    console.log('started listening on port 3000');
});

As you can see we have one node package 'feed' installed, you can get the documentation for it here. This package does all the magic of converting your data to RSS xml format.

That's all you need to do.

Output

Following is the rendered RSS xml you will get.


<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
   <channel>
       <title>Example</title>
       <description>An interesting site which talks sense</description>
       <link>http://example.com</link>
       <lastBuildDate>Thu, 05 May 2016 11:31:32 GMT</lastBuildDate>
       <docs>http://blogs.law.harvard.edu/tech/rss</docs>
       <generator>Feed for Node.js</generator>
       <category>Technology</category>
       <category>Programming</category>
       <category>Web Development</category>
       <item>
           <title><![CDATA[Post 1]]></title>
           <link>http://example.com/post-1</link>
           <guid>http://example.com/post-1</guid>
           <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
           <description><![CDATA[Post 1 summary]]></description>
       </item>
       <item>
           <title><![CDATA[Post 2]]></title>
           <link>http://example.com/post-2</link>
           <guid>http://example.com/post-2</guid>
           <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
           <description><![CDATA[Post 2 summary]]></description>
       </item>
       <item>
           <title><![CDATA[Post 3]]></title>
           <link>http://example.com/post-3</link>
           <guid>http://example.com/post-3</guid>
           <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
           <description><![CDATA[Post 3 summary]]></description>
       </item>
       <item>
           <title><![CDATA[Post 4]]></title>
           <link>http://example.com/post-4</link>
           <guid>http://example.com/post-4</guid>
           <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
           <description><![CDATA[Post 4 summary]]></description>
       </item>
   </channel>
</rss>