'How to run service in Express.js regardless of recieved requests [duplicate]

I have a backend application made in nodeJS and express. The main app is divided into separate file, app.js and index.js. app.js is used for managing express, adding its controllers and connecting to MongoDB. index.js is there just for creating server.

Now I would like to implement service, that will check the current state in database, and in case of true, run some code, or function. And that code should be running all the time, regardless of sent requests. It should be a code for itself. So if anyone could tell me what is the best way of implementing this kind of behaviour.

Thank you!



Solution 1:[1]

There can be multiple ways. I am sharing two of them.

  1. Using Scheduling methods of JS which allows you to run some code in a time interval.
  2. Using a Job Manager or scheduler. This article will help you to understand how to build a queue-based job manager using redis.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shohidul Bari