简体   繁体   English

如何在 NextJs 中运行服务器端脚本?

[英]How to run server-side scripts in NextJs?

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.我是 NextJs 的新手,我正在尝试制作一个应用程序,每隔几个小时就会向数据库发出一些请求以更新数据,但我不知道该怎么做。 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.我唯一可以做到的地方是在页面组件内部,但这意味着必须有来自客户端的请求才能触发更新脚本,但我希望它在服务器启动时每隔几个小时连续执行一次之后。

I think you have two options you can either use vercel's cron method.我认为您有两种选择,您可以使用 vercel 的 cron 方法。 https://vercel.com/docs/concepts/solutions/cron-jobs 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.我更喜欢的是,在我的app.ts (app.js)中使用一些setInterval逻辑,它只是一个快速服务器起点。 It will keep fetching the data with setInterval .它将继续使用setInterval获取数据。 we might use some pm2 logic here as well.我们也可以在这里使用一些pm2逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM