【问题标题】:node js schedule cron job to call express route every week节点js安排cron作业每周调用快递路线
【发布时间】:2021-03-12 19:25:46
【问题描述】:

我有一个页面,当您访问它时,它会运行测试。我希望我的测试每周运行一次,所以我试图创建一个每周访问我的快速路由的 cron 作业。有点像我提出了一个获取请求。

为了测试,我有一个每 2 分钟运行一次的 cron 作业:


//schedule job every 2 minutes
schedule.scheduleJob("*/2 * * * *", function () {
    console.log('inside cron function')
});

router.get('/my_page_route_name', ensureAuthenticated, async function(req, res){
    res.render('my_page_route_file', {
        layout: 'dashboard.handlebars',
        jsMain: 'my_page_route_js',
    });
});

如果我将我的网址输入到http://localhost:1337/my_page_route_name,它就可以进入 router.get 请求。但是有没有一种方法可以触发我的 cron 作业来调用相同的路由并每 2 分钟呈现一次页面?

我不确定如何执行此操作,因为 router.get 函数使用 res.render,而且我的 cron 作业中没有 res 变量

{{ 编辑 }}

我的 cron 作业有效并触发对我的路由的 POST 请求:

    schedule.scheduleJob("*/10 * * * *", async function() {
        console.log('inside cron function');

        const resp = await fetch("http://localhost:1337/my_page_route_name/", {
            "headers": {
                "content-type": "application/json"
            },
            "method": "post",
            "body": JSON.stringify({
                "username":"exvar",
                "password":"examplevar2"
            })
        });
    
    });

我创建了一个快速路由来接收 POST 请求;


router.post('/my_page_route_name', async function(req, res){
    

    res.render('my_page_route_name_html', {
        layout: 'dashboard.handlebars',
        jsMain: 'my_page_route_name_jsmain',
       
    });
})

如果我在邮递员中发出请求,我可以看到 posr 路由返回网页 html,但没有运行任何脚本,例如我的 html 文件中有 <script> document.querySelector('.breadcrumbs').append('[[ html loadded ]]') </script> 被加载,但代码似乎不是在我收到的回复中运行

【问题讨论】:

  • 您是否尝试每 2 分钟获取一次路线?
  • 最终我会让它在每个星期二获取路线,为了进行测试,我让它每 2 分钟运行一次,这样当我在本地运行它时我可以看到结果。我的 cron 每 2 分钟运行一次,我可以在本地机器上看到 console.log,我现在只是想弄清楚如何获取我的路线是的

标签: javascript node.js express cron


【解决方案1】:

在节点中使用 fetch 包,因为 http 请求很快就会变得相当复杂。

const fetch = require('node-fetch');

//schedule job every 2 minutes
schedule.scheduleJob("*/2 * * * *", async function() {
    const response = await fetch('https://yourdomain.tld/my_page_route_name');
    const body = await response.json();

    console.log('inside cron function', body);
});

router.get('/my_page_route_name', ensureAuthenticated, async function(req, res){
    res.render('my_page_route_file', {
        layout: 'dashboard.handlebars',
        jsMain: 'my_page_route_js',
    });
});

【讨论】:

  • 谢谢,fetch 工作,但似乎没有在页面上运行任何脚本?例如,我在我的 html 文件中添加了<script> document.querySelector('.breadcrumbs').append('[[ html loadded ]]') </script>,如果我在浏览器中正常加载 url,我可以看到页面上的文本。我的 cron 作业使用 POST 请求获取网页,该请求命中我创建的快速 POST 路由,并返回页面的 html 内容但没有运行我的脚本标记,因为我可以看到我的文本未插入响应中(更新我的问题 rn)
  • 我很困惑。你希望服务器执行你的js吗?您在此处寻找的 fetch 的响应是什么?
  • 是的,我正在寻找我的响应来执行一些 js。当我在本地访问浏览器中的 url 时,它会执行 js 来查询要在网页中显示的值。如果值被破坏,它会发送一封警报电子邮件。所以我试图让我的页面每天都被一个 cron 作业访问,该作业正常运行 js,就好像有人访问了该页面一样
  • 这不是服务器和浏览器的工作方式。除非执行 SSR,否则服务器无法执行浏览器。也许使用playwright.dev ???
猜你喜欢
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
相关资源
最近更新 更多