简体   繁体   中英

How to store the data I've fetched from a third-party API in my express server and schedule another API call at a later time?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM