【问题标题】:Trying to access https URL using Node.js尝试使用 Node.js 访问 https URL
【发布时间】:2017-02-07 23:17:36
【问题描述】:

我正在尝试使用 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

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js xml url https ssl-certificate


    【解决方案1】:

    我使用的是 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)
        }
    })
    

    希望有人会觉得这很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多