2016-07-28 10 views
1

こんにちは、私はスケジュールでhttpコールをしようとしています。スケジュールの最初のオカレンスは、2番目の出現からうまく動作します。空のオブジェクトを取得していて、定義されていません。nodejs再帰パターンのhttp要求

var https = require('https'); 
var schedule = require('node-schedule'); 
var rule = new schedule.RecurrenceRule(); 
rule.second = 10; 
schedule.scheduleJob(rule, function(){ 

    var options = { 
    host: 'google.co.uk', 
    method: 'get', 
    path: '/' 
}; 

var req = https.request(options, function(res) { 
    // some code 
    console.log('Expity date is ' + res.socket.getPeerCertificate(true).valid_to); 


}); 
req.end(); 
}); 

出力は次のとおりですが、スケジュールとして実行したときに出力されないのはなぜですか?いくつかの痛みを伴うデバッグ要求オブジェクトは、接続を開始するためにエージェントを使用して、以前に確立された接続を使用し、再度証明書を要求していないことが判明した後

Expity date is Oct 5 13:16:00 2016 GMT 
Expity date is undefined 

答えて

0

。したがって、行動。これを解決するには、 "agent"をオプションとして使用し、それをfalseに設定します。

以下の変更されたコード。

var https = require('https'); 
var schedule = require('node-schedule'); 
var rule = new schedule.RecurrenceRule(); 
rule.second = 10; 
schedule.scheduleJob(rule, function(){ 

var options = { 
    host: 'online.hmrc.gov.uk', 
    method: 'get', 
    path: '/', 
    agent: 'false' // essential to close down previous conncetion pools and make a fresh call. 
}; 
options.agent = new https.Agent(options); // Initialise a new agent with options as its parameters. 
var req = https.request(options, function(res) { 
    // some code 
    console.log('Expity date is ' + res.socket.getPeerCertificate(true).valid_to); 


}); 
req.end(); 
});