2016-07-28 71 views
1

您好我正在嘗試按計劃進行http調用。計劃中的第一次出現工作正常,從第二次發生我得到空物體和未定義。遞歸模式下的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

經過一番痛苦的調試發現,請求對象使用一個代理來發起連接,並使用先前建立的連接和證書又沒有要求。因此,行爲。要解決它,請使用「代理」作爲選項並將其設置爲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(); 
});