expressjsHow do I set up a cron job using Express.js?
Setting up a cron job using Express.js is a simple process. The following example code will demonstrate how to set up a cron job that runs every minute:
const cron = require('node-cron');
cron.schedule('* * * * *', () => {
// Do something every minute
});
The code above contains the following parts:
-
const cron = require('node-cron');
- This line imports thenode-cron
module. -
cron.schedule('* * * * *', () => {
- This line sets up a cron job that runs every minute. The first parameter is a string that contains the time pattern for the job. The*
character indicates that the job should run every minute. -
// Do something every minute
- This line is the code that will be run every minute. -
});
- This line closes the callback function that will be run every minute.
For more information about setting up cron jobs with Express.js, please refer to the following links:
More of Expressjs
- How do I find Express.js tutorials on YouTube?
- How do I render a template using Express.js?
- How can I disable the X-Powered-By header in Express.js?
- How can I use Express.js with TypeScript?
- How do I implement CSRF protection in an Express.js application?
- How can I use the x-forwarded-for header in Express.js?
- How do I manage user roles in Express.js?
- How do I use adm-zip with Express.js?
- How do I use Express.js with W3Schools?
- How can I use Zipkin to trace requests in Express.js?
See more codes...