Node.JS Programming
April 28, 2022
How To Produce An Rss Feed In Node.js
by: Underminer

Show people what you've got, when you've got it


What is RSS:

RSS stands for Really Simple Syndication. It’s a computer readable format, served as XML, that allows reader software to poll a site to determine if new content is available, and retrieve at least some related content. Most readers function much like email clients, but retrieve content like news posts or blog entries in place of mail messages.

Why should I bother:

Ultimately, it’s important for the end user to have options and be able to choose how they consume their content.

With people becoming increasingly frustrated by being spoon fed content in the order and way that the tech giants decide, there are groups of people deciding to kick the algorithms to the curb and gain control of how they ingest content again. One of the strategies people are using is a return to various aggregator and reader software that can consolidate a number of news and content sources. In that arena RSS is king, and having a solution in place allows you to service those individuals.

What are the components of a feed:

If you examine the example below, you’ll notice that as part of your rss object you define a channel with the following components, which should be fairly self explanatory:

  • Title
  • Link
  • Description
  • Language

Then, before closing the channel definition, you can include several “Item” listings. These are your posts/blog entries, etc. Each item can have several components as well. These are mostly self explanatory as well, but with a couple gotchas:

  • Title - Self explanatory. The title of the post/entry/resource
  • pubDate - The date of publication. This must be in rfc822 date format.
  • Link - The FQDN URL path to the post
  • Guid - Thus must be in the form of a full URL unless permalink is set false. Easiest to use the same data as for the link
  • Category - You can have multiple category tags, most readers use them to assign tags to posts
  • Description - The description of the content and/or the accepted place for html formatted material. If sending HTML encase in _cdata tag.
  • Content - Optional, and despite what you might think NOT the accepted place to place html formatted material, but acceptable for text content.

How to do it:

var xml = require ("xml");

const sitetld = “https://yoursitetld.com”

const sitedescription = “This is my site. Enjoy”

 

async function createfeed2(feedtitle, posts){

  const xmlObject = {

      rss: [

          {

              _attr: {

                  version: '2.0',

                  'xmlns:atom': 'http://www.w3.org/2005/Atom'

              }

          },

          {

              channel: [

                  {

                      'atom:link': {

                          _attr: {

                              href: sitetld + '/rss/mainfeed.rss',

                              rel: 'self',

                              type: 'application/rss+xml'

                          }

                      }

                  },

                  { title: sitetitle + " - " + feedtitle },

                  { link: sitetld },

                  { description: sitedescription },

                  { language: 'en-us' },

                  ...posts.map((post) => {

                      const absoluteHREF = sitetld + post.articlelink

                      const contentdata = post.contentdata

                      return {

                          item: [

                              { title: post.title },

                              { pubDate: post.published_at.toUTCString() },

                              { link: absoluteHREF },

                              { guid: absoluteHREF },

                              { category: post.categoryname },

                              { description: { _cdata: contentdata } }

                          ]

                      }

                  })

              ]

          }

      ]

  }

  const xmlString = '' + xml(xmlObject)

  return xmlString;

}

 

What’s Happening?

In the above example, we’re passing our collection of posts to a function to create the xml output we need using the excellent xml npm package to format said output.

Since we’re constructing and passing an object to xml, we can’t use a for loop to iterate over the posts, and instead have to use the map method.

Note that you can string other functions in line to prepare the posts collection as needed, or you can alter the above example to use the values you use elsewhere.

If you’re using express you could either pass the resulting return straight to your res.send() function, cache it with something like Redis, or you could write the resulting xml to a file and place with your statically served assets. If you’re using an alternate webserver solution, you may be best served with either caching the asset with Redis, or writing to a statically served xml file.