How to run server-side scripts in NextJs?

2.4k Views Asked by At

i am new to NextJs and I'm tryin to make an app that every few hours make some requests to a database to update the data, but i can't figure out how to do it. The only place where i can do it is inside the pages components, but that means that there has to be a request from a client in order to trigger the update script but i want it to be executed continuously when the server starts and every few hours after that.

1

There are 1 best solutions below

2
Sohail Haider On

I think you have two options you can either use vercel's cron method. https://vercel.com/docs/concepts/solutions/cron-jobs

What I'm more comfortable with is, using some setInterval logic inside my app.ts (app.js) where its mere a express server starting point. It will keep fetching the data with setInterval. we might use some pm2logic here as well.