Configure Cron Jobs in Nodejs

Configure Cron Jobs in Nodejs

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 :

  1. Install node-cron :

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);        
No alt text provided for this image
asterisks with different unit of time :





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]

回复

要查看或添加评论,请登录

Digvijay Jadhav的更多文章

  • Non-functional Requirements in software development

    Non-functional Requirements in software development

    Non-functional requirements (NFR) may not be visible to users and customers directly, but their absence may impact the…

    3 条评论
  • Have you ever heard of Fuzzy Search?

    Have you ever heard of Fuzzy Search?

    Have you heard about term "fuzzy search" ? Many of my unanswered questions were answered a few months ago when I…

  • Code Documentation: A Guide for Beginner's??

    Code Documentation: A Guide for Beginner's??

    In this blog we’ll be having a quick look into how to document the code. Before we get into how to properly document…

    6 条评论

社区洞察

其他会员也浏览了