递归模式下的nodejs http请求

nodejs http request in recursive pattern

您好,我正在尝试按计划进行 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

经过一番调试发现request对象使用代理发起连接,并使用之前建立的连接,不再请求证书。因此行为。要解决它,请使用 "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();
});