DEV Community

Cover image for How to Schedule Cron Job in Node.js + Express.js
Sohail SJ | TheZenLabs
Sohail SJ | TheZenLabs

Posted on

63 3 2 2 2

How to Schedule Cron Job in Node.js + Express.js

So, last week a task came across to send sales executives their target reports on Friday at 5.00 PM.
You might have encountered such tasks where you must schedule a task to run at a specific time.

So in this blog, I will show you how to schedule a task in Node.js using node-cron package.

Prerequisites

  • Basic knowledge of JavaScript
  • Basic knowledge of npm
  • Basic knowledge of Node.js and Express.js

Lets break down the task into smaller steps.

  1. Create a new Node.js project
  2. Install express and node-cron package
  3. Create a src folder and create a new file index.js inside it
  4. Create a new route to test the functionality
  5. Schedule the task to run the functionality

Step 1: Create a new Node.js project

mkdir node-cron-example
cd node-cron-example
npm init -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install express and node-cron package

npm install express node-cron
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a src folder and create a new file index.js inside it

mkdir src
cd src
touch index.js
Enter fullscreen mode Exit fullscreen mode

For windows users, you can use type nul > index.js to create a new file.

Step 4: Create a new route to test the functionality

// index.js
const express = require('express');
const app = express();

app.get('/send-target-reports', (req, res) => {
    console.log('Sending target reports to sales executives');
    res.send('Target reports sent successfully');
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

Verify the router by opening route http://localhost:3000/send-target-reports in your browser.

Step 5: Schedule the task to run the functionality

// index.js
const express = require('express')
const app = express()
const cron = require('node-cron')

const generateTargetReports = () => {
  console.log('Generating target reports')
  console.log('Target reports generated successfully')
  console.log('Sending target reports to sales executives')
  console.log('Target reports sent successfully')
}

app.get('/send-target-reports', (req, res) => {
  console.log('Sending target reports to sales executives')
  res.send('Target reports sent successfully')
})

app.listen(3000, () => {
  console.log('Server is running on port 3000')
})

cron.schedule('0 17 * * 5', generateTargetReports)
Enter fullscreen mode Exit fullscreen mode

In the above code, we have scheduled the task to run on Friday at 5.00 PM using cron.schedule('0 17 * * 5', generateTargetReports). The first parameter is the time when the task should run and the second parameter is the function that should run at that time.

Break down of cronjob schedule

  • 0 stands for minutes
  • 17 stands for hours ie 5.00 PM
  • * stands for any day of the month
  • * stands for any month
  • 5 stands for Friday

Additional Resource

You can find more about the cronjob schedule here

Conclusion

That's it. You have successfully scheduled a task to run on a specific time in Node.js using node-cron package. I hope you find this blog helpful. If you have any questions, feel free to ask in the comments below or contact me on Twitter @thesohailjafri

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (2)

Collapse
 
mdarbaz98 profile image
Arbaaz Shaikh •

Nice one brother 😃

Collapse
 
thesohailjafri profile image
Sohail SJ | TheZenLabs •

Thanks, brother, let's keep learning together

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up