如何在NextJ中运行服务器端脚本?
我是NextJ的新手,我正在尝试制作一个应用程序,每隔几个小时就会向数据库提出一些请求以更新数据,但是我不知道该怎么做。 我唯一可以在页面组件内完成它的地方,但这意味着为了触发更新脚本,客户必须有一个请求,但是我希望在服务器启动时连续执行它,并且每隔几个小时就可以连续执行在那之后。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您有两个选择,可以使用Vercel的Cron方法。 https://vercel.com/docs/docs/conepts/conepts/solutions/solutions/solutions/cron-jobs
我更舒服的是,在我的
app.ts(app.js)
中使用一些setInterval
逻辑,其中它仅是Express Server起点。它将继续使用setInterval
获取数据。我们可能还可以在此处使用一些pm2
逻辑。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 myapp.ts (app.js)
where its mere a express server starting point. It will keep fetching the data withsetInterval
. we might use somepm2
logic here as well.