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

发布于 2025-01-29 04:46:36 字数 148 浏览 4 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2025-02-05 04:46:36

我认为您有两个选择,可以使用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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文