I am working on an Angular(frontend) and NodeJS(APIs) application. I want to schedule email on particular dates without any external call. I explored node-schedule. But how should I ensure that it runs forever in my NodeJs APIs? Like where should I put the code - in app.js or give it a route?
5 Answers
You are on the right track. You have to use cron service for this. And node-schedule
is a good choice for this.
So first, make a file named email-service.js
.
Inside that put your logic.
email-service.js
var node = require('node-schedule');
var sendEmail = node.scheduleJob('0 6 * * *', function(){
console.log('Starting..');
init(); // write your logic here to send email
});
function init() {
console.log('Your logic goes here.');
}
module.exports = {
cronService: cronService
}
app.js
In the app.js import email-service.js
.
const emailService = require('email-service')
emailService.sendEmail.start(); // start service..
You can schedule a cron accordingly. Below is the format of the cron.
The cron format consists of:
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

- 6,755
- 3
- 34
- 52
-
Hi thanks for the help. I am using new Date() as the parameter of schedulejob and the year, month and date value are fetched from database. Doing this results in mails being sent continuously. But if I pass direct values in new Date() it works as intended. Please Help. – Divesh Soni Mar 19 '19 at 10:50
-
Also please explain the exports – Divesh Soni Mar 19 '19 at 11:11
-
exports is an object. So it exposes whatever you assigned to it as a module. – Surjeet Bhadauriya Mar 19 '19 at 12:57
-
I am working on how to make it cron work dynamic. Once I am done with it, I'll post here. – Surjeet Bhadauriya Mar 19 '19 at 12:58
-
@SurjeetBhadauriya hey man can you please take a look at here: https://stackoverflow.com/questions/66987333/start-stop-cronjob-on-button-click-in-nodejs-express-app . I badly need help with this. – Zak Apr 07 '21 at 16:58
-
Hey @SurjeetBhadauriya, do you have any clue how to handle the timezone conversation? Currently, if I choose in my front-end (Vuejs) a date when an email to be sent, it will automatically convert it in UTC on the back-end. This means, that the message will be sent earlier (in my case 2 hours earlier, cause I am in EEST) and that is not correct. – Vitomir Feb 03 '23 at 12:59
One more thing, if the application is restarts then your schedule event get cancelled. so it might be a good approach if you save your event in a db and marked them complete or incomplete. And re-schedule your incomplete events at the restart of the application.
I use this to make sure all events runs.

- 175
- 1
- 2
- 10
Sounds like something you can achieve with nodeCron.
You may have a look at the answer here too
You can shcedule your scheduler to execute on a specific time like
var schedule = require('node-schedule');
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
The example above is from node-cron npm page. Please have a look and their github for better understanding.

- 4,756
- 17
- 65
- 116
You can use node-schedule node module, it's well documented.

- 1
-
1please provide more detail, maybe an example. OP would find your answer of little help, he already mentioned he is considering node-schedule. – AJS Mar 18 '19 at 05:58
We use PM2 (https://pm2.io/runtime/) - It's a process manager that keeps your process running and has configuration that will allow you to schedule a node job to run according to a cron schedule. There's alot more to it, but if you're looking to improve the operation of your API, this tool is really good - and free.

- 26
- 3