2016-07-28 67 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(); 
});