简体   繁体   中英

How to register hapi cron with the latest version of hapi.js

I have my server registered with hapijs.

Here is my index.js

composer((err, server) => {
  if (err) throw err
  server.initialize((errInit) => {
    if (err) throw errInit
    const register = (hapiRaven, options, next) => {
      return next()
    }
    register.attributes = {
      name: 'hapi-raven',
      version: '1.0.1',
      options: { }
    }
    server.register([ register ])
    server.start(() => {
      // server starts
    })    
  })
})

I have to use hapi-cron and need to call an api with it for every minute. So I need to know where I can use below register option in the above code which is working in the 16th hapi version.

{
      register: require('hapi-cron'),
      options: {
        jobs: [{
          name: 'Workout Every minute',
          time: '* * * * *',
          timezone: 'America/Los_Angeles',
          request: {
            method: 'GET',
            url: `/workout-reminder?type=everyMinute`,
          },
          callback: (res) => {
            console.info('Workout Every minute')
          }
        }]
      }  
    }

The server.register() takes an array of plugins. You can register the hapi-cron plugin the same way you have registered hapi-raven .

composer((err, server) => {
    if (err)
        throw err
        server.initialize((errInit) => {
            if (err)
                throw errInit
                const register = (hapiRaven, options, next) => {
                    return next()
                }
            register.attributes = {
                name: 'hapi-raven',
                version: '1.0.1',
                options: {}
            }
            const hapiCron = {
                register: require('hapi-cron'),
                options: {
                    jobs: [{
                            name: 'Workout Every minute',
                            time: '* * * * *',
                            timezone: 'America/Los_Angeles',
                            request: {
                                method: 'GET',
                                url: `/workout-reminder?type=everyMinute`,
                            },
                            callback: (res) => {
                                console.info('Workout Every minute')
                            }
                        }
                    ]
                }
            }
            server.register([register, hapiCron])
            server.start(() => {
                // server starts
            })
        })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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