Skip to main content
Node.JS Cron Jobs

How To Periodically Call a Function in Nodejs

In this tutorial, I will create node app to run a method periodically.You can also use linux cron job to call a function periodically but not for windows. I am creating a nodejs express server and added a rest call, which will call on each 5 minutes.

The node cron is a npm package that help to create cron type functionality in nodejs. If you don’t want any external package for this then you need to use $interval service.

How To run Cron Jobs in Nodejs

Lets’ create an express app and schedule a job that will run every minutes, the job could be any method, rest service etc.Created 'test-cron' folder in any location of the computer, Also created below files into this folder –

  • package.json : This file contains all package information or dependencies.
  • main.js : This file contains express server and app functionality.

Install Nodejs Dependencies

The package.json file is responsible to take-care all nodejs dependencies. Open test-cron/package.json file and added below code into this file –

{
  "name": "node-restapi",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": {
    "name": "Adam",
    "email": "[email protected]",
    "url": "http://js-tutorials.com/"
  },
  "license": "MIT",
  "dependencies": {
    "axios": "^0.19.0",
    "body-parser": "^1.16.1",
    "cors": "^2.8.5",
    "express": "^4.14.1",
    "node-cron": "^2.0.3"
  }
}

I used node-cron package for cron job setup into node.js application.I also used express cors package for Cross-Origin Resource Sharing. Now open command line and run below code –
$test-cron > npm install
Above command installed all dependencies which are mentioned into package.json file.

Create Nodejs Server

We will open main.js file and add below code into this file.

var express = require('express')
  , path = require('path')
  , app = express()
  ,cors = require('cors')
  ,axios = require('axios');

app.use(cors());
const PORT = process.env.PORT || 3010;
app.set('port', process.env.PORT || 3010);
app.use(express.static(path.join(__dirname, 'public')));
 app.listen(PORT, function () {
  console.log('Node.js server is running on port ' + PORT);
});
function getEmployee() {
axios.get('https://dummy.restapiexample.com/api/v1/employee/1')
  .then(response => {
    console.log(response.data);
  }).catch(error => {
    console.log(error);
  });
}

I have created express server and run into 3010 port, I have also use axios for http client request, and created getEmployee() method to get data from rest api.

Schedule Cron Job in Nodejs

imported node-cron package using require and call schedule() method, that will take cron job schedule time and call callback method as a parameter.

var cron = require('node-cron');
cron.schedule('*/5 * * * *', () => {
  console.log('running a task in 5 minutes');
  getEmployee();
})

Leave a Reply

Your email address will not be published. Required fields are marked *