How to Schedule Cron Job in Node.js + Express.js
Published: 2024-03-15 18:10:04

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](#step-1-create-a-new-nodejs-project)
2. [Install `express` and `node-cron` package](#step-2-install-express-and-node-cron-package)
3. [Create a src folder and create a new file `index.js` inside it](#step-3-create-a-src-folder-and-create-a-new-file-indexjs-inside-it)
4. [Create a new route to test the functionality](#step-4-create-a-new-route-to-test-the-functionality)
5. [Schedule the task to run the functionality](#step-5-schedule-the-task-to-run-the-functionality)

### Step 1: Create a new Node.js project
```bash
mkdir node-cron-example
cd node-cron-example
npm init -y
```

### Step 2: Install `express` and `node-cron` package
```bash
npm install express node-cron
```

### Step 3: Create a src folder and create a new file `index.js` inside it
```bash
mkdir src
cd src
touch index.js
```
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
```javascript
// 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');
});
```
Verify the router by opening route `http://localhost:3000/send-target-reports` in your browser.

### Step 5: Schedule the task to run the functionality
```javascript
// 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)
```

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](https://crontab.guru/)

## 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](https://twitter.com/thesohailjafri)