What is RSS?

1.RSS stands for Really Simple Syndication

2.RSS gives you the ability to syndicate your web site’s content

3.RSS defines a very simple way to share and view headlines and content

4.RSS files can be automatically updated

5.RSS allows you to personalize views for different web sites

6.RSS is written using XML. The above quote is from runoob.com

During the process of searching for information, I found that this article is very detailed and can be used as an in-depth understanding of RSS.

How RSS works

RSS is used to share information between websites. With RSS, you register your content with a company called an aggregator.

One of the steps is to create an RSS document and save it with the.xml suffix. Then upload this file to your website. Next, register through an RSS aggregator. Every day, the aggregator searches the registered site for RSS documents, validates links, and displays information about the feed so that customers can link to documents that interest them.

How do I add RSS to my blog?

The basic format of RSS is very simple, as shown in the following example:

<? The XML version = "1.0" encoding = "utf-8"? > < RSS version="2.0"> <channel> <title> <link>https://fishbone.live/</link> <description>Fishbone Technology blog < / description > < item > < title > based on an Egg. The increased js blog RSS function < / title > < link > https://fishbone.live/article/9 < / link > <description> What is RSS? < description > < item > < item > < title > blog add comments < / title > < link > https://fishbone.live/article/10 < / link > </description> </item> </channel> </ RSS >Copy the code

This blog is using egg.js to add RSS function steps are as follows:

1. Find the latest 10-20 articles

async getTop10Articles() {
  try {
    const result = await this.app.mysql.select('ARTICLE', { 
      columns: ['id', 'uniqueMark', 'intro', 'title', 'createTime', 'publishTime' ],
      where: { isPublished: 1 },
      orders: [['publishTime','desc']],
      limit: 10,
      offset: 0,
      })
    return result
  } catch(error) {
    const { CUSTOM_ERROR, ERROR_TYPE } = this.ctx.helper
    this.ctx.throw(new CUSTOM_ERROR(ERROR_TYPE.SERVER_ERROR, error))
  }
}
Copy the code

2. Dynamically produce RSS XML documents according to the queried articles

const fs = require('fs') const { Feed } = require('rss2') const writeXML = function(xml, ctx) { return new Promise(resolve => { fs.writeFile('./app/public/rss.xml', xml, (err) => { if (! err) resolve({ result: 'ok' }) else ctx.logger.error(err) }) }) } const posts = await ctx.service.customized.article.getTop10Articles() posts.forEach(post => { const { title, uniqueMark, intro, createTime } = post const postUrl = `${address}article/${uniqueMark}` const author = [ { name: `${name} Yu`, email, link: address } ] feed.addItem({ title: title, id: postUrl, link: postUrl, description: intro, content: intro, author: [author], date: new Date(createTime), }) }) const writeXMLresult = await writeXML(feed.rss2(), ctx) return writeXMLresultCopy the code

3. Click the RSS icon to navigate to the rss.js page, invoke the interface of Step 2 (dynamically generating RSS XML data), and jump to the generated RSS. XML file.

const getRss = function() {
  return new Promise(resolve => {
    axios.get('/api/rss').then(res => {
      resolve(res);
    })
  })
}
const Page = () => {
  useEffect(() => {
    const getxml = async () => {
      const res = await getRss()
      if (res.data.result === 'ok') location.href = '/rss.xml'
    }
    getxml()
  }, [])
  return <div>loading RSS XML</div>
}
Copy the code

If the RSS plugin is installed in your browser, you will see the following subscription window when you jump to rss.xml:It’s a success! Other visitors can subscribe when they get your RSS feed, get notified when new articles are published, and they don’t have to open your site.