Configure Cron Jobs in Nodejs
Digvijay Jadhav
Technology Head | System Architect | Leads Engineering Team @ Scrobits Technologies
Have you ever desired to execute a certain job on your application server at specific intervals without having to physically do it? Alternativelywe'd prefer you focus your time on more important tasks than remembering to clean or transfer data from one part of the server to another on a regular basis.
We can use cron job schedulers to automate such tasks. There may be repetitive tasks such as logging and performing backups that need to occur on a daily or weekly or monthly basis.
One method for implementing?cron?on a Node.js server is by using the?node-cron?module.
Let's say you want to create a script that sends an email to your subscribers every Monday at 10am. Here are the steps to achieve this :
npm install cron
2. Require the packages :
const cron = require('node-cron')
const nodemailer = require('nodemailer');
3. Defining the function for email sending using nodemailer :
function sendEmail()
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: '[email protected]',
pass: 'sender-email-password'
}
});
let mailOptions = {
from: '[email protected]',
to: '[email protected], [email protected]',
subject: 'Monday Newsletter : Title ',
text: 'Hi there, Good Morning !! This is your weekly newsletter!'
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sucessfully sent: ' + info.response);
}
});
};
4. Schedule the email sending job
领英推荐
//This will run the sendEmail function every Monday at 10am (UTC time)
cron.schedule('0 10 * * 1', sendEmail);
5. Start the scheduler :
cron.start();
That's it!!
The script will now send an email to your subscriber every Monday at 10 a.m. You may personalise the email text by modifying the message object, and you can schedule the Cron Job at a different time or interval by changing the schedule variable.
Here's complete code :
const cron = require('node-cron');
const nodemailer = require('nodemailer')
function sendEmail()?
? let transporter = nodemailer.createTransport({
? ? host: 'smtp.gmail.com',
? ? port: 587,
? ? secure: false,
? ? auth: {
? ? ? user: '[email protected]',
? ? ? pass: 'sender-email-password'
? ? }
? });
? let mailOptions = {
? ? from: '[email protected]',
? ? to: '[email protected], [email protected]',
? ? subject: 'Monday Newsletter : Title ',
? ? text: 'Hi there, Good Morning !! This is your weekly newsletter!'
? };
? transporter.sendMail(mailOptions, function(error, info) {
? ? if (error) {
? ? ? console.log(error);
? ? } else {
? ? ? console.log('Email sucessfully sent: ' + info.response);
? ? }
? });
};
//This will run the sendEmail function every Monday at 10am (UTC time)
cron.schedule('0 10 * * 1', sendEmail);
cron.start();
Dear consultancy and agencies, we are hiring for Java Technologies. Please let us know if anyone ready to empanel with us. Also can kindly share your hotlist to us at?[email protected]