I want to create an api based on Node.js and Express.js. This api should return an array of products on /products
. To get these products on my node server, I need to call a third party api. However, I don't want to call the third-party API every time a client calls the API I provide, because these products only need to be updated once a day. So I want clients to call the API route frequently and internally call the third party API only once a day.
My assumption is to use a package like node-cron to call the API once a day and then update the product array I send on an API call. This would basically look like this.
let products = []
cron.schedule('0 0 * * *', () => {
let response = await fetch(/* third party api */)
products = response
});
app.get('/products', function (req, res) {
res.send(products)
})
I'm not sure if this is a good way to solve this use case, or if there are more sophisticated ways to solve this?