Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use request/response with in node cron job

Tags:

node.js

cron

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");
like image 917
Aishwarya Karthik Avatar asked Sep 13 '25 00:09

Aishwarya Karthik


1 Answers

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");
like image 169
Tuan Anh Tran Avatar answered Sep 20 '25 09:09

Tuan Anh Tran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!