April 20, 2016

Add rss feed to your node.js website

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.


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) {
            title: p.title,
            link: "http://example.com/" + p.slug,
            description: p.summary,
            date: p.pubdate

    //add categories

    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');

    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">
        <description>An interesting site which talks sense</description>
        <lastBuildDate>Thu, 05 May 2016 11:31:32 GMT</lastBuildDate>
        <generator>Feed for Node.js</generator>
        <category>Web Development</category>
            <title><![CDATA[Post 1]]></title>
            <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
            <description><![CDATA[Post 1 summary]]></description>
            <title><![CDATA[Post 2]]></title>
            <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
            <description><![CDATA[Post 2 summary]]></description>
            <title><![CDATA[Post 3]]></title>
            <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
            <description><![CDATA[Post 3 summary]]></description>
            <title><![CDATA[Post 4]]></title>
            <pubDate>Thu, 05 May 2016 11:31:32 GMT</pubDate>
            <description><![CDATA[Post 4 summary]]></description>
Thanks for reading. Copyright © notabot.in