Running Node.js Code on a Timer

Hey guys,

For my current project I need to make 3-4 API calls to seperate endpoints(this is required to get the data I want). Instead of having this done in response to a call from my frontend (this call can take up to 10s), I would prefer to have Node.js do this on a timer and save it to my database. The data is dynamic so I cannot just get it once.

My question is, can i just stick something like this in my startup (./bin/www) express file? I plan on pushing this to Heroku, this code would only need to run every 6 hours or so. I just tested this on my local server and it seems to work but I’m wondering if anyone here has experience in this sort of thing.

setInterval(function() {
    //do something
}, some interval);


Let Heroku handle the scheduling. Write out the task you want to schedule in a separate module. You can set up a scheduler to run your script at regular intervals. It can seem confusing at first, but there isn’t actually much to it. Check out this tutorial:

1 Like

Awesome, thanks again man!