Member-only story
How to run a Cron job using NextJs/Vercel Framework
If you are not subscribed to medium membership, you can visit link here:
Nextjs is a React framework to build static web pages and also for serverless developement. NextJs has file based router mechanism for which we will use the route using the external cron service. The API created through NextJs will be used by a external cron service.
I will show you an simple example:
To create a boilerplate for sample nextjs project, run the following command:
npx create-next-app
let’s name it sample-cron-app
. After creating a project, navigate to the project directory and run npm run dev
and hit the url: http://localhost:3000/api/hello
you will see the event log in the console as shown below:
Now, lets create a file named app.js and install cron package by using npm i cron
and http request package (I used node-). The code is shown below:
const { CronJob } =…