2017-02-07 32 views
0

我試圖使用Node.js從HTTPS URL訪問XML數據集。我使用了與使用HTTP URL的其他數據集相同的代碼,並且工作正常。嘗試使用Node.js訪問https 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); 
    } 
}) 

這是我在命令行中收到的錯誤。 enter image description here

任何幫助將不勝感激。

回答

0

相反,我使用的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) 
    } 
}) 

希望有人會覺得這非常有用。

相關問題