I'm looking for a way to check the subscriptions of our users with a cron job daily. I want to use node-cron for this. I'm not sure where I should put the code to start the cronjob. Currently, I have it set up like this:
app.js:
app.listen(config.port, function() {
// Logging initialization
console.log('--');
console.log(chalk.green(config.app.title));
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
console.log(chalk.green('Port:\t\t\t\t' + config.port));
console.log(chalk.green('Database:\t\t\t\t' + config.db.uri));
if (process.env.NODE_ENV === 'secure') {
console.log(chalk.green('HTTPs:\t\t\t\ton'));
}
console.log('--');
if (callback) callback(app, db, config);
var job1 = new cron.CronJob({
cronTime: '* * * * *',
onTick: function(){
app.get('/api/users/checkSubscriptions/').success(function() {
console.log( 'working!')
}).error(function() {
console.log( 'error!')
});
},
start: false,
timeZone: 'Europe/Paris'});
job1.start();
});
users.server.routes.js
'use strict';
module.exports = function(app) {
app.route('/api/users/checkSubscriptions/')
.get(subscriptionCheck.checkSubscriptions);
};
cronjob-subscription.server.controller
var mongoose = require('mongoose'),
User = mongoose.model('User');
exports.checkSubscriptions = function() {
User.findAll(function(err, users) {
if(err) {
console.log(err);
} else {
for(var i = 0;i< users.length; i++) {
var user = users[i];
console.log(user);
}
}
});
}
Currently I just want to print the users to the console to check if this is working. With the current code in app.js I get the following error:
app.get('/api/users/checkSubscriptions/').success(function() {
^
TypeError: app.get is not a function
Could you please advice how I can do the API call in the cronjob when initialising the app?