« Job Scheduling in Node.js

Introduction

Here will learn how to create a job in node JS using node-cron. Job scheduling allows you to schedule arbitrary code (methods/functions) to execute at a fixed date/time, at recurring intervals, or once after a specified interval.

Prerequisites

  1. Need a Node.js installed on your local machine.
  2. Have a basic knowledge of Node.js.
  3. Have a basic knowledge of the Express framework.
  4. Be familiar with sending emails through nodemailer.

Step- 1. Creating a Node.js Application

Create a directory and give it a name node-job-schedule(You can also give name as per your choice). Then open the directory you have created using your favorite code editor. Now open the terminal and type:

1npm init -y

Add the express framework, node-cron module, and node-mailer module by typing on terminal:

1npm install express node-cron nodemailer

Step-2. Creating The Server

Create the server that we will use for scheduling tasks. Create an index.js file in our directory(node-job-schedule).

Import the express module and initialize it:

1const express = require('express');
2const app = express();

Now set the server to listen to port 3001. (You can set port number as you wish)

1app.listen(3001, () => {
2 console.log('Server started at port 3001');
3});

Step -3. Creating The Email Function

Let’s create the function that will send emails. First, import the node-mailer module:

1const mailer = require('nodemailer');

Then create a transporter through nodemailer’s createTransport method.

1const transporter = mailer.createTransport({
2 host: 'smtp.gmail.com',
3 port: 587,
4 auth: {
5 user: 'kumaranishdubey2022@gmail.com',
6 pass: '****************',
7 },
8});

Use this link for pass

https://stackoverflow.com/questions/45478293/username-and-password-not-accepted-when-using-nodemailer

Now create a function and give it a name of your choice, I have used a name sendEmail. In the function, we will use the transporter’s sendMail method to send the email.

1function sendEmail(message) {
2 transporter
3 .sendMail({
4 from: '"Anish" <kumaranishdubey2023@gmail.com>',
5 to: '"Anish" <kumaranishdubey2022@gmail.com>',
6 subject: 'Scheduled Job Email',
7 text: message,
8 })
9 .then((_) => {
10 console.log('Email sent on ' + new Date());
11 })
12 .catch((error) => {
13 console.log(error);
14 });
15}

Step -4. Scheduling a Task

Import the node-cron module:

1const cron = require('node-cron');

In this step, we are going to use the schedule method we created above to schedule the sendEmail function. We will schedule the function to run after every 10 minutes.

1const cron = require('node-cron');
2// cron run at every 10 min
3cron.schedule('*/1 * * * *', function () {
4 sendEmail('Hey User, this email was sent to you automatically');
5});

Step -5. Run the Server and Check.

Now open your terminal in the project root folder, run the following command:

1node index.js

Result:

Server started at port 3001

Email sent on Thu Nov 03 2022 21:27:04 GMT+0530 (India Standard Time)

Email sent on Thu Nov 03 2022 21:28:03 GMT+0530 (India Standard Time)

For full working code please visit

https://github.com/anishakd4/node-job-schedule