Aishwarya Karthik
Aishwarya Karthik

Reputation: 53

How to use request/response with in node cron job

Hi i am a beginner in node js. I need to use the cron to mail the user about the pending payment. I am using the module Cron . The url /remainder performs all the neccessary function to do the job and is working perfectly fine. My doubt is can I call this url within the cron job. How can we use the request/response. When the cron is run, the output is null .

var CronJob = require('cron').CronJob;
new CronJob('00 * 23 * * *', function(req,res){
    console.log(req);


}, null, true, "America/Los_Angeles");

Upvotes: 4

Views: 7610

Answers (1)

Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

So /remainder is doing the actual job right? Just call it inside callback.

var request = require('request')
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
    console.log('You will see this message every second');

    request('http://www.google.com', function(error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('im ok')
            // console.log(body) // Show the HTML for the Google homepage.
        }
    })
}, null, true, "America/Los_Angeles");

Upvotes: 2

Related Questions