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.

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;

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.

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.definetbl_news is passed. Next database table columns are mapped. Mapped columns are title as String, description as String and publish_date as Date.

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.

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.

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

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.

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 🙂

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? 🙂

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?