How to have my NodeJS app run in a loop for ever
<p>I have written a NodeJS app that calls an API and posts to an endpoint only on weekdays at a specific time.</p>

<p>I could setup a cron job to run the app at the specified time but I'd prefer to run it with <code>node index.js</code> and have it run constantly, doing nothing until it's the right day and time and then going back to "sleep" until the following day.</p>

<p>How do I achieve that? I tried with a while loop:</p>

<pre><code>while (true) {
  myApp.run();
}
</code></pre>

<p>Obviously that didn't go too well.</p>

<p>What's the right way to do it? Should I rewrite my modules to use events, so that I emit one when it's time and there is a listener that reacts to it?</p>

<p>--edit: To be more specific, I would like it to run in a similar way to an app that has a webserver in it. When you start the app, it's running and waiting for connections; it doesn't exit when the request &amp; connection end, it stays running waiting for more requests &amp; connections.</p>

<p>--edit 2: The reason I'd rather not use a cron is because the days and time to run on are configurable in a <code>config.json</code> file that the app parses. I'd rather avoid messing with <code>cron</code> and just change the schedule by editing the <code>config.json</code> file.</p>

<p>--edit 3: I'd like to code this myself and not use a module. My brain always hurts when trying to write an app that would run forever and I'd like to understand how it's done rather than using a module.</p>

<p>--edit 4: Here is what I ended up using:</p>

<pre><code>function doStuff() {
  // code to run
};

function run() {
  setInterval(doStuff, 30000);
};

run();
</code></pre>