Fetch RSS Feed Using RSS to JSON API
Tags: api, webdev
Published: 2024-02-23 14:17:22

Hi devs, 

Let's quickly build a Node.js application that transforms RSS feeds into JSON format using the RSS to JSON API. This tutorial will guide you through setting up a simple server with Express, creating a user-friendly HTML form for submitting RSS feed URL. 

Pre-requisite:

-  Secret API Auth Token ( You can obtain this from [ApyHub's RSS to JSON API](https://apyhub.com/utility/converter-rss-json) )
-  Node.js installed on your system.

### Step 1: Set Up Your Project

- **Create a new directory** for your project and navigate into it:
   ```bash
   mkdir rss-fetcher
   cd rss-fetcher
   ```

- **Initialize a new Node.js project** by running:
   ```bash
   npm init -y
   ```

- **Install required packages**. You'll need `express` for creating the server and `axios` for making HTTP requests:
   ```bash
   npm install express axios
   ```

### Step 2: Create the Server and HTML Form

- **Create a file named `server.js`** in your project directory.

- **Add the following code to `server.js`**. This code sets up an Express server with a simple HTML form that allows users to input an RSS URL. It also includes a route to handle the form submission and fetch the RSS feed as JSON using the API.

   ```javascript
   const express = require('express');
   const axios = require('axios');
   const app = express();
   const port = 3000;

   app.use(express.urlencoded({ extended: true })); // Middleware to parse form data

   app.get('/', (req, res) => {
     // HTML form for user input
     res.send(`
       <h2>RSS to JSON Converter</h2>
       <form action="/fetch-rss" method="post">
         <label for="rssUrl">RSS URL:</label><br>
         <input type="text" id="rssUrl" name="rssUrl" value=""><br><br>
         <input type="submit" value="Convert">
       </form> 
     `);
   });

   app.post('/fetch-rss', async (req, res) => {
     try {
       const response = await axios.post('https://api.apyhub.com/convert/rss-url/json?detailed=true', {
         url: req.body.rssUrl,
       }, {
         headers: {
           'Content-Type': 'application/json',
           'apy-token': 'YOUR_APY_TOKEN_HERE'
         }
       });
       res.json(response.data); // Send the converted JSON back to the client
     } catch (error) {
       res.status(500).send('Error fetching the RSS feed');
     }
   });

   app.listen(port, () => {
     console.log(`Server running at http://localhost:${port}`);
   });
   ```

   Replace `'YOUR_APY_TOKEN_HERE'` with your actual API token.

### Step 3: Run Your Application

- **Start your server** by running:
   ```bash
   node server.js
   ```

- **Open a web browser** and navigate to `http://localhost:3000`. You'll see a form where you can enter the URL of an RSS feed.


- **Enter the RSS URL** you want to convert and click "Convert". The server will send the URL to the RSS to JSON API, convert the feed to JSON, and display the JSON response in the browser.


That's it! This simple Node.js application allows users to convert RSS feeds to JSON format using a web form. You can extend this application by adding error handling, supporting more form fields for additional API parameters, or styling the form with CSS.