[英]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.我正在构建一个电影评论应用程序来了解更多关于 React 和一般编程的信息。 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/ ).现在,我已经成功地将 API 与前端集成,但我注意到因为我在 localhost 上打开了项目,当我编辑和保存时,网站会刷新,从而创建另一个对提供程序的 API 调用( 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.
我不喜欢这种行为,所以我想创建一个快速服务器并在那里创建我的 API 调用,并让我的前端与该服务器通信。
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?我的想法是,当第一次从前端查询提供者时,我会对提供者进行 API 调用,然后将来自提供者的响应存储在某个变量中? 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.
因此,当我的前端向我的 express 服务器查询相同的调用时,我可以简单地发送这个预取的数据,而不是进行另一个调用。 I would also like it so that my express server calls the API at set intervals so that the data is up-to-date.
我也希望我的快速服务器以设定的时间间隔调用 API,以便数据是最新的。
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:我的文件夹结构目前是这样的:
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 .
但是,如果您的数据很大,那么最好将结果本地存储在数据库或文本文件中,或者使用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).只需
setInterval
用于“强制”从 API(而不是本地缓存)获取数据。
// 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.