'Looking for a Nodemon alternative for MongoDB

I wrote a little server that fetches stock market & crypto data from Yahoo-finance - then stores it onto MongoDB. I would like to refresh the server and update the data once in a while. I used Nodemon to do so, but it doesn't seem to acknowledge the changes in variables ( for stock/crypto prices ).

Is there any other way to do so?

I heard that the only way is to write JS code on the client side to refresh the server with AJAX. Is that the best way to go about it ?

// IMPORTS
const request = require("request-promise");
const fs = require("fs");
const mongoose = require("mongoose");
const cheerio = require("cheerio");
const Stock = require("./model/Stock");
const express = require("express");
const cors = require("cors");
const app = express();


async function connectToMongoDB() {
    await mongoose.connect("mongodb+srv://*****:****@cluster0.ctjxj.mongodb.net/Cluster0?retryWrites=true&w=majority");
    console.log("connected to mongodb");
};

//WEB-SCRAPE || FETCH CURRENT STOCK DATA
async function tsla() {


    const html = await request.get(
        "https://finance.yahoo.com/quote/BTC-USD/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();


    await connectToMongoDB();
        /*
    const listingModel = new Tsla({
        price: Price, 
        symbol: Symbol, 
        percentChange: PercentChange,
    });
    */

    // await listingModel.save();

    await Stock.findOneAndUpdate(
        {
            symbol: "Tesla (TSLA)",
        }, 
        {
        price: Price, 
        symbol: "Tesla (TSLA)", 
        percentChange: PercentChange,
        },
        {
            upsert: true
        }
        );

    
    
        console.log(Price)
    setTimeout(tsla, 30000); //60 seconds == 1minute


    
   
}

tsla();

async function aapl() {


    const html = await request.get(
        "https://finance.yahoo.com/quote/AAPL/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();


    await connectToMongoDB();

 /*   const listingModel = new Aapl({
        price: Price, 
        symbol: Symbol, 
        percentChange: PercentChange,
    });

    await listingModel.save();

    */

    await Stock.findOneAndUpdate(
        {
            symbol: "Apple (AAPL)",
        }, 
        {
        price: Price, 
        symbol: "Apple (AAPL)", 
        percentChange: PercentChange,
        },
        {
            upsert: true
        }
        );
    
    

    console.log(Price)
    setTimeout(aapl, 30000); // 60,000 = 60 seconds == 1minute
    
}

aapl();


async function spy() {


    const html = await request.get(
        "https://finance.yahoo.com/quote/spy/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();


    await connectToMongoDB();

    console.log(Price)
    setTimeout(spy, 30000); // 60,000 = 60 seconds == 1minute

    /*const listingModel = new Spy({
        price: Price, 
        symbol: Symbol, 
        percentChange: PercentChange,
    });

    await listingModel.save(); */

    await Stock.findOneAndUpdate(
        {
            symbol: "SPDR S&P 500 ETF (SPY)",
        }, 
        {
        price: Price, 
        symbol: "SPDR S&P 500 ETF (SPY)", 
        percentChange: PercentChange,
        },
        {
            upsert: true
        }
        );

}

spy();

async function nvda() {


    const html = await request.get(
        "https://finance.yahoo.com/quote/nvda/"
        );

    const $ = await cheerio.load(html);

    const Price = $('[class="Fw(b) Fz(36px) Mb(-4px) D(ib)"]').text();
    const Symbol = $('[class="D(ib) Fz(18px)"]').text();
    const PercentChange = $('fin-streamer[class="Fw(500) Pstart(8px) Fz(24px)"][data-field="regularMarketChangePercent"]').find('span').text();

    await connectToMongoDB();

    console.log(Price)
    setTimeout(nvda, 30000); // 60,000 = 60 seconds == 1minute


   /* const listingModel = new Nvda({
        price: Price, 
        symbol: Symbol, 
        percentChange: PercentChange,
    });

    await listingModel.save(); */

    await Stock.findOneAndUpdate(
        {
            symbol: "NVIDIA (NVDA)",
        }, 
        {
        price: Price, 
        symbol: "NVIDIA (NVDA)", 
        percentChange: PercentChange,
        },
        {
            upsert: true
        }
        );
}

nvda();
 

//MIDDLEWARE
app.use(express.json());
app.use(cors());
app.use(express.urlencoded({extended: true}));

// DATABASE

app.get("/", (req, res) => {
    Stock.find({}).then(
        items => res.json(items)
    ).catch(err => console.log(err));
});

app.listen(3001, function () {
    console.log("Server is running...");
});


Solution 1:[1]

When you say I would like to refresh the server and update the data once in a while i assume you want to fetch data from yahoo finance and update the values on MongoDB. Why dont you just try Cron JobsCron Job Example. A cron job is basically a piece of code that is run automatically by the server every X amount of time. So, you can create a cron job that fetches data from yahoo finance every 30mins and update your database.

I am just confused though with Nodemon that you tried to use. Nodemon is just a package that helps you on development and automatically restarts your server when you make changes on your scripts. I has nothing to do with fetching data, it is for developing purposes only.

As for the AJAX call from the frontend to a basckend service that will fetch your data. Yes, it is achievabled that way but it is REALY BAD practice and it is not the only way. Imagine this, to make a AJAX call from your frontend to your backend that means that someone (a user) MUST type the specific URL of your web page that will make the request to the server. What happens if no one goes to that page? There will be no request and thus no updated data. Lets again say that the web page that makes the request is your index so no one can avoid it when they visit your website. What if 100 users visit your site at the same time ? are you going to make at the same time 100 request to your server ? and if so, will you fetch 100 times the same data from yahoo finance that they possibly did not change ? That why this approach is the worst in your case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1