Configuring Timed Functions using NodeJs, Express, and Node-Cron.

Created by eneaslari 17/8/2023

main

Step 1: Create a NodeJs application.

Initialize a new app with npm and install the Express package. Make sure NodeJs is installed on your machine.

  • npm init

  • npm i express

Step 2: Set up the Express server

  • Create the app.js file (note that some people prefer to call this file server.js)

  • Configure the port and set up the listener

const express = require("express");
const app = express();
app.set("port", process.env.PORT || 3000);

app.listen(app.get("port"), () => {
  console.log("Express server listening on port " + app.get("port"));
});

Step 3: Add the node-cron npm package to the application

  • npm i node-cron

Step 4: Set up a folder for your scheduled jobs

  • Create a folder called "scheduledFunctions" or similar

Step 5: Define the logic to run your scheduled jobs

  • In the folder you just set up, create a file to hold your scheduled function logic. You can also use different files for each task if you prefer.

  • Configure scheduled logic

const CronJob = require("node-cron");

exports.initScheduledJobs = () => {
  const scheduledJobFunction = CronJob.schedule("*/5 * * * *", () => {
    console.log("I'm executed on a schedule!");
    // Add your custom logic here
  });

  scheduledJobFunction.start();
}

alt text

The execution frequency of your custom function is defined as the first argument to the Cron.schedule function. Note that this argument is written as a Cron Schedule Expression or crontab. I find Crontab Guru a really useful resource to play around and get comfortable with schedule expressions so you can easily set up your own.

Step 6: Configure the Express app to initialise and run the job schedules

  • In the app.js file add a call to the init function(s) from your scheduled file(s).
const express = require("express");
// DEFINE the path to your scheduled function(s)
const scheduledFunctions = require('./scheduledFunctions');
const app = express();
app.set("port", process.env.PORT || 3000);

// ADD CALL to execute your function(s)
scheduledFunctions.initScheduledJobs();

app.listen(app.get("port"), () => {
  console.log("Express server listening on port " + app.get("port"));
});

Start the app to generate the functions and their schedules in the Cron scheduler. Your custom logic will be executed to schedule as long as the app is running.

Sources:

Link 1

Link 2

Link 3

Link 4

More to read


Designing Fun and Engaging Levels for My Space-Themed Match-3 Game
8/2/2025

Designing levels for my space-themed match-3 game has been a journey of balancing **fun, challenge, and variety**. In this article, I share my experience with **creating engaging puzzles, managing difficulty, and keeping gameplay fresh**. From playtesting to strategic layouts, these insights shape my approach to making levels that players will love. 🚀

From Burnout to Balance: Rediscovering Energy in Game Dev, Fitness, and Life
7/2/2025

A reflection on how burnout, illness, and lost momentum forced me to rethink my approach to productivity, motivation, and balance. Now, I’m refactoring my life—one habit at a time—to rebuild my energy without falling into the same cycle again.

New Year, New Code: My Developer Resolutions for the Year
29/12/2024

In this blog post, I share my New Year's resolutions as a developer: posting more about my work on social media, writing more articles, and finally finishing and publishing a game. Plus, I offer practical tips on setting realistic and achievable goals to help fellow developers kick off the year with purpose!

Bad Practices in JavaScript: Common Pitfalls and How to Avoid Them 🚫
28/12/2024

JavaScript is like the Swiss Army knife of web development—it’s everywhere, from tiny website features to massive, complex web apps. But with great power comes... well, the chance to make a mess! Its flexibility and loose rules make it super easy to use, but also super easy to misuse.