Cron Jobs are used for scheduling tasks to run on the server.CRON Jobs are the most commonly used method to automate tasks on Server. In this article, we will see how to schedule JOBS in Node.js. We are going to use node-schedule package for scheduling JOBS.
node-schedule is a light cron-like scheduler Package for Node JS.
Installing node-schedule
npm install node-schedule
Setting up Scheduler
var scheduler = require('node-schedule'); var rule = new scheduler.RecurrenceRule(); rule.hour = 7 rule.dayOfWeek = new schedule.Range(0,6) var dailyJob = schedule.scheduleJob(date, function(){ console.log('I run on days at 7:00'); }); scheduler.scheduleJob(rule,dailyJob);
Canceling a Scheduled Job on Node
dailyJob .cancel();
You can read more about the node-schedule package from https://github.com/node-schedule/node-schedule
Also, Read Node js interview questions