Home > Net >  How to store the data I've fetched from a third-party API in my express server and schedule ano
How to store the data I've fetched from a third-party API in my express server and schedule ano

Time:07-06

I'm building a movie review app to learn more about React and programming in general. I was initially following a tutorial but I realized that the approach of "sitting down and waiting for them to type and explain things as I code along" didn't suit me. I decided to make the project on my own with the things I've learned from the tutorial thus far, and simply look up on google the things that I want to include in my project, too.

Right now, I've successfully integrated the API with the frontend but I have noticed that because I have the project open on localhost, as I edit and save, the website refreshes thereby creating another API call to the provider (https://www.themoviedb.org/). I do not like this behaviour so I thought to create an express server and create my API calls there and have my frontend communicate with this server instead.

My thinking was that I'll make an API call to the provider when it is first queried from the frontend and then store the response from the provider somewhere perhaps in a variable? So that when my frontend queries the same call to my express server, I can simply send this prefetched data instead of making another call. I would also like it so that my express server calls the API at set intervals so that the data is up-to-date.

Can someone help me how to go about doing this? Thank you!

Here's the code I currently have on the express server:


// index.js
const express = require("express")
const app = express()
const port = process.env.PORT || 3001
const cors = require("cors")
require("dotenv").config()

const featuredMovie = require("./services/getfeaturedMovie.js")

app.get("/get-featured-movie", (req, res) => {
    console.log(featuredMovie)
})

app.use(cors)
app.listen(port, () => {
    console.log(`Mobiusflix server is listening on port ${ port }`)
})

My folder structure is currently this:

  • node_modules
  • api
    • getFeaturedMovies.js <- I only have this function for now
  • .env
  • index.js

CodePudding user response:

The node server keeps listening and running as a process. You can store values in variables and they will exist as long as the server is up. However if your data is big then it would be best to store the result locally in database or a text files or using node-cache.

As for setting up interval to refresh the data, sure. Just setInterval for "force" fetch the data from API (rather then local cache).

// usually cache will be an object with methods like get(), set()
// we just use local var
var cache = {};

app.get('/my-api/workers', function(req, res, next) {

  if (cache["workers"]) {
    res.send(cache["workers"]);
  } else {
    var url = "https://www.external-api.com/v1/workers";
    fetch(url).then(response => {
      cache["workers"] = response.data;
      res.send(cache["workers"]);
    })
  }
});

setInterval(function() {
  delete cache["workers"];
}, 60 * 1000)

  • Related