im running an express api on a vm with nodejs and using pm2. I have a function running on setInterval and running every 30 seconds, the function gets data from an external api and then im concatenating the fetched data from the interval to the previous fetched data. I am importing the previous data ( const data = require('data.json') ).. Then i am using fs.writeFileSync to update the json file.
Then in my express routes, i am importing the json data file and sending the json data as the response. The problem im having is when i call the express route to get the data, the data being sent isn't the updated json file and does not include the lastest data from all the intervals. When i restart the app with pm2, then it then will send the lastest data. Can anyone help? Thanks