2017-02-07 3 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) 
    } 
}) 

うまくいけば、誰かがこれは役に立つでしょう。

関連する問題