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.
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.
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:
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:
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; } |
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.