How to generate RSS Feed using NodeJS and MySQL

Generate RSS Feed using NodeJS

RSS (Rich Site Summary) is a way of delivering dynamically changing content. RSS is used to share information on internet mostly by blogs, news sites and publishers. In a previous tutorial we discussed how to generate xml using node js. In this tutorial you are going to learn how to generate RSS feed using NodeJS and MySQL.

generate rssfeed using nodejs

In this tutorial you will learn to.

1. Create a database and a table for news and insert sample news data.

2. Generate an express application to generate rss feed using NodeJS.

3. Use Node orm module to fetch data from database.

4. Use feed module to generate rss feed using nodejs .

5. Use fs module to save generated rss file.

To complete this tutorial you need to first install NodeJS and MySQL on your system.

Install NodeJS and MySQL

Visit NodeJS website to download latest version of NodeJS. After download run NodeJS installer. To install MySQL and PHPMyAdmin go to WAMP or XAMPP website. Download and install XAMPP.

Create News database and table

Run WAMP or XAMPP. Open PHPMyAdmin and Click SQL tab. Execute queries given below.

CREATE DATABASE IF NOT EXISTS `dbnews` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;

USE `dbnews`;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_news`
--

CREATE TABLE `tbl_news` (

  `id` int(11) NOT NULL,

  `title` varchar(600) NOT NULL,

  `description` text NOT NULL,

  `publish_date` date NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tbl_news`
--

INSERT INTO `tbl_news` (`id`, `title`, `description`, `publish_date`) 

VALUES
(1, '''Guardians of the Galaxy Vol. 2'' Rockets to No.1 with $145 million', 'LOS ANGELES, (Variety.com) - And just like that, Star-Lord and his band of super buddies are back on top of the box office.', '2017-05-08'),

(2, 'Mars Science Lab launch delayed two years', 'NASA''s launch of the Mars Science Laboratory -- hampered by technical difficulties and cost overruns -- has been delayed until the fall of 2011, NASA officials said at a news conference Thursday in Washington.', '2008-05-09'),

(3, 'Nasa runs competition to help make old Fortran code faster', 'Two coders will share a $55,000 prize for what a Nasa official calls the "ultimate ''geek'' dream assignment.', '2017-05-04'),

(4, 'Retro tech: Flying car prototypes', 'How flying cars have gone from being a science fiction fantasy to almost becoming a reality, as seen through the BBC archive.', '2017-05-03'),

(5, 'Antarctic iceberg crack develops fork', 'The fissure that will lead to the breakaway of one of the largest bergs ever seen has a new branch.', '2017-05-02'),

(6, 'Secrets of tea plant revealed by science', 'Botanists unlock the genetic workings of the tea plant, in a move that could improve flavour.', '2017-05-01'),
(7, 'Machine learning', 'Many people are unsure about what machine learning is, but the chances are they are using it every day.', '2017-04-25'),

(8, 'In the Future, Virtual Assistants Will Also Have Ideas of Their Own', 'Assistants could proactively recommend ways in which humans could be living better lives, from getting more exercise to watching informational videos.', '2017-05-06');


ALTER TABLE `tbl_news`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `tbl_news`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

generate rss feed using nodejs

Create an express app to generate rss feed using nodejs

Visit NodeJS website, download and install nodejs. Next install express-generator. Open command line and execute following command.

$ npm install express-generator -g

Express generator is installed using NPM or Node Package Manager. NPM is used to install modules. Express generator installs Express tool. Express tool is used to generate a skeleton for NodeJS Express applications. Default templating engine is pug.

express --view=pug generate-rssfeed-using-nodejs

generate rss feed using nodejs

You can view the generated application above.

Install dependencies

To install required modules, type

cd generate-rssfeed-using-nodejs & npm install

Above command changes directory to project directory and installs required modules using npm.

Install Feed module

Now after basic express app is setup. First install feed module to generate rss feed using nodejs. On command line, run following command.

npm install feed --save

Install Node ORM Module

After feed module installation, let us install Node ORM module. Node ORM module is an object relational mapping module, that is used to manipulate databases.

npm install node-orm --save

Open application folder in you favorite IDE like sublime text or brackets.

generate RSS feed using nodejs

Open index.js file in routes directory and replace all code with the code given below.

var express = require('express');

var router  = express.Router();

const Feed  = require('feed')

var orm     = require('orm');

var fs      = require('fs');

var dirPath = __dirname + "/../public/rss/rssfeed.xml";

router.use(orm.express("mysql://root:@localhost:/dbnews", {
  
define: function (db, models, next) {
    models.news = db.define("tbl_news", {
          
               title        : String,
  		
               description  : String,
  		
                publish_date : Date
    });

  	next();

  }
}));

let feed = new Feed({

  title: 'Generate RSS feed wtih NodeJS',

  description: 'Generate RSS feed wtih NodeJS',

  author: {

    name: 'Programmer blog',

    link: 'http://programmerblog.net/'

  }

})

/* GET home page. */

router.get('/', function(req, res, next) {

  var result = req.models.news.find({
  }, function(error, news){
  
    if(error) throw error;

      for(var i=0; i< news.length; i++){

         var myFormattedDate = news[i]['publish_date'].toISOString().substring(0, 10);

         var newarr = myFormattedDate.split('-');

         var year   = newarr[0];

         var month  = newarr[1];

         var day    = newarr[2];
         
         feed.addItem({

              title: news[i]['title'],

              link:"http://localhost/news/"+news[i]['id'],

              description: news[i]['description'],

              author: [{

                name: 'Programmer Blog',

                email: 'janedoe@example.com',

                link: 'http://programmerblog.net'

              }],

              date: new Date(year,month, day),

            });

        }

        var rssdoc = feed.rss2();

        fs.writeFile(dirPath, rssdoc, function(err) {

           if(err) { 

               return console.log(err); 

           }

       });

      res.render('index');

  });

});

module.exports = router;

Include orm, feed and fs modules

First feed module is included to generate rss feed from database records. Secondly orm module is included to connect and fetch records from database. fs module is used to save generated RSS feed to a file.

const Feed  = require('feed')

var orm     = require('orm');

var fs      = require('fs');

Path to save RSS feed

Open public folder and create a folder named rss. In index.js define path to save generated rss file. RSS file is saved with the name rssfeed.xml.

var dirPath = __dirname + "/../public/rss/rssfeed.xml";

Connect to database using Node ORM and create a model

Applcation connects to database using orm’s express method inside router.use method. Username and password with database name are passed, in second parameter in orm.express, a model is defined inside define and a function that accepts database object, models and a parameter next.

Model for news database table is defined. Inside db.define tbl_news is passed. Next database table columns are mapped. Mapped columns are title as String, description as String and publish_date as Date.

router.use(orm.express("mysql://root:@localhost:/dbnews", {
  
define: function (db, models, next) {
    models.news = db.define("tbl_news", {
          
               title        : String,
  		
               description  : String,
  		
                publish_date : Date
    });

  	next();

  }
}));

Fetch data from database using Node ORM

After connection to database and model creation, next task is to fetch data from database.

router.get('/', function(req, res, next) {

  var result = req.models.news.find({
  }, function(error, news){ ... });

Inside ‘/’ router req.models.news.find method is used to fetch data. An anonymous function is defined that receives err object and news result set. After result is returned, we loop through it to add news items to feed.

for(var i=0; i< news.length; i++){ .. }

Format publish date

Inside loop, publish_date is formatted using .toISOString method and substring 10 characters to get year, month and day.

var myFormattedDate = news[i]['publish_date'].toISOString().substring(0, 10);

         var newarr = myFormattedDate.split('-');

         var year   = newarr[0];

         var month  = newarr[1];

         var day    = newarr[2];

Create a feed of news items

To create a feed of news items, feed.addItem method is used. A news item is passed as an object with parameters like title, link and description with author’s name, email and link to author’s profile, last parameter is date.

feed.addItem({

              title: news[i]['title'],

              link:"http://localhost/news/"+news[i]['id'],

              description: news[i]['description'],

              author: [{

                name: 'Programmer Blog',

                email: 'janedoe@example.com',

                link: 'http://programmerblog.net'

              }],

              date: new Date(year,month, day),

Generate RSS feed using nodejs

After all news items are added to feed object, feed’s rss2 method is called. This method generates RSS feed and assigns RSS feed to a variable rssdoc.

var rssdoc = feed.rss2();

        fs.writeFile(dirPath, rssdoc, function(err) {

           if(err) { 

               return console.log(err); 

           }

       });

      res.render('index');

Save rss feed to a file

To save generated RSS feed to a file, fs module’s writeFile method is used and full path with file name and RSS document are passed. Finlay index view is rendered and a success message is displayed to user.

Running the app

In order to run the app, open command line and type.

set DEBUG=generate-rss-feed-using-nodejs:* & npm start

Now open browser and type URL given below in address bar. RSS feed is generated and saved in specified path.

http://localhost:3000

generate rssfeed using nodejs

Summary

To summarize, in this tutorial you learned to generate rss feed using nodejs. Feed and orm modules are used to fetch data from database and generate RSS feed. You can download example source code by using this download link.

Run npm install on command line to install all required modules before running example code.

Please leave you feedback and comments. Follow us on twitter to stay informed about upcoming articles and tutorials.

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

  • Nice write buddy!
    Do you have any idea why does it takes so much time to re-generate the rss feed? If there is any changes in the database, on the next reload nothing changed. When I’m just simply reload the page with F5, I’ve got back 304 response code, then with Shift+F5 got back 200. Although nothing changed into the feed, sometimes after restart the app or sometimes randomly worked. Bit hectic 🙂

    permissions seems also OK
    -rw-r–r– 1 rss rss 5046 Jul 25 09:50 public/rss/rssfeed.xml

    Thnx!

    • Hi János ,
      Thanks for your comments and appreciation. I am looking into the issue. As I fix it will let you know

      Regards,

      • Hi Jason,

        i’ve found the problem. The xml re-generate happening only in that case when you call index.js. In caseif you call only just the rssfeed.xml nothing change. Do you have any idea how can the xml call trigger the rebuild of itself? 🙂

  • Hi Jason,

    i’ve found the problem. The xml re-generate happening only in that case when you call index.js. In caseif you call only just the rssfeed.xml nothing change. Do you have any idea how can the xml call trigger the rebuild of itself?