Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.3 KiB

  1. One of the beautiful things about Node is that it is really easy to do just about anything in a few lines of code.
  2. To put this in perspective, it took me longer to make this *terrible* blog post header than it did for me to implement an RSS feed in node.
  3. An RSS (rich site summary) feed enables people to subscribe to blogs and get notified when there is a new post.
  4. People also use RSS feeds to aggregate all the blogs they read in one place.
  5. Although RSS is on the decline, it is still widely used in the tech community.
  6. Before looked for a package I added a route listening on "/rss" which sends a static object that will eventually store the RSS feed object.
  7. ```javascript
  8. routes.get('/rss', (request, result) =>
  9. {
  10. result.set('Content-Type', 'text/xml');
  11. result.send(xmlFeed);
  12. });
  13. ```
  14. The next step was to find a nifty node package that handles the generation of RSS XML.
  15. I'm using the [package](https://www.npmjs.com/package/rss) adequately named "RSS".
  16. ```bash
  17. npm install -s rss
  18. ```
  19. Based on the documentation for the package, I initialized the RSS generator object.
  20. ```
  21. // defines basic details about your blog
  22. var feed = new RSS({
  23. title: 'jrtechs',
  24. description: 'Jeffery\'s blog which has everything from data-science to cooking',
  25. feed_url: 'https://jrtechs.net/rss',
  26. site_url: 'https://jrtechs.net',
  27. image_url: 'https://jrtechs.net/includes/img/favicon/android-chrome-512x512.png',
  28. docs: 'https://github.com/jrtechs/NodeJSBlog',
  29. language: 'en',
  30. categories: ['other', 'hardware', 'open-source', 'programming', 'projects', 'web-development', 'data-science'],
  31. });
  32. var xmlFeed = feed.xml();
  33. ```
  34. The final step was to add all the recent posts to the RSS feed.
  35. ```javascript
  36. const sql = require('../utils/sql');
  37. sql.getRecentPosts().then((posts)=>
  38. {
  39. posts.forEach(post =>
  40. {
  41. feed.item({
  42. title: post.name,
  43. url: "https://jrtechs.net/" + post.category + "/" + post.url,
  44. date: post.published
  45. });
  46. });
  47. xmlFeed = feed.xml();
  48. }).catch((err)=>
  49. {
  50. console.log(err);
  51. });
  52. ```
  53. Although my implementation is probably very rudimentary, it is flexible and implementing it myself gave me a deeper appreciation for RSS.
  54. Looking through the package documentation, I discovered a multitude of details that RSS feeds could contain that I didn't know about.