尝试使用 Node.js 访问 https URL

Trying to access https URL using Node.js

我正在尝试使用 Node.js 从 HTTPS URL 访问 XML 数据集。我对使用 HTTP URL 的不同数据集使用了相同的代码并且工作正常。

我已经调查过这个 https://www.npmjs.com/package/ssl-root-cas 但我不知道我应该怎么做。

这是我正在使用的代码。

var parseString = require('xml2js').parseString;
var https = require('https');

function xmlToJson(url, callback) {
    var req = https.get(url, function(res) {
        var xml = '';
        res.on('data', function(chunk) {
          xml += chunk;
        });
        res.on('error', function(e) {
          callback(e, null);
        }); 
        res.on('timeout', function(e) {
          callback(e, null);
        }); 
        res.on('end', function() {
          parseString(xml, function(err, result) {
            callback(null, result);
          });
        });
    });
}

var urlTrain = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7602&format=xml";
xmlToJson(urlTrain, function(err, data) {
    if (err) {
        return console.err(err);
    }
    else{
        console.log(data);
        console.log(data.busstopinformation);
        console.log(data.busstopinformation.results);
        console.log(data.busstopinformation.results.result[0]);
        console.log(data.busstopinformation.results.result[0].shortname);
    }
})

这是我在命令行中收到的错误。 Link

任何帮助将不胜感激。

相反,我使用了 JSON 版本并使用了以下代码:

var request = require("request")
var url = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=4566&format=json"

request({
    url: url,
    json: true,
    rejectUnauthorized: false
}, function (error, response, body) {

    if (!error) {
        console.log(body) // Print the json response
    }
    else{
        console.log(error)
    }
})

希望有人会觉得这有用。