如何在 vuejs 中从 node-cron 安排任务

问题描述 投票:0回答:2

这是根据 node js 中的文档使用它的方法,每个 * 都有它的作用

cron.schedule('* * * * *', () => {
  console.log('running a task every minute');
});

 # ┌────────────── second (optional)
 # │ ┌──────────── minute
 # │ │ ┌────────── hour
 # │ │ │ ┌──────── day of month
 # │ │ │ │ ┌────── month
 # │ │ │ │ │ ┌──── day of week
 # │ │ │ │ │ │
 # │ │ │ │ │ │
 # * * * * * *

例如,我希望每 10 秒发送一封邮件,我会这样做

cronJob.schedule('10 * * * * *', () => {
})

我如何从我的 vue 组件中获取 10 秒并将其传递给我的 cron 作业

node.js vue.js cron node-cron
2个回答
0
投票
cron.schedule('*/10 * * * * *', () => {

 // other code goes here 

})

//每 10 秒运行一次


0
投票

要从 node-cron 脚本发送电子邮件,您需要 nodemailer npm 包。之后,您必须通过将包导入您的 node-cron 文件并指定消息的发送详细信息、传输器和邮件选项来配置您的电子邮件:

    var transporter = nodemailer.createTransport({
   service: 'gmail',
    auth: {
           user: 'yoursendingmail',   
          pass:'yoursendingpassword'              
         },
        const mailOptions = { 
                  from: '[email protected]', 
                  to: '[email protected],          
                  subject: 'sub here',  
                  html: '<p>text here</p>'}
 };)

之后,你像这样设置 node-cron:

cronJob.schedule('10 * * * * *', () => {
  transporter.sendMail(mailOptions, function (err, info)
    {
      if(err){}
      else{}//can log info here if email is sent
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.