在Node.js中从URL中检索JSON的最好方法是什么?

所以我一直在使用sailsjs从外部网站请求json数据,然后将这些数据发布到创buildpath。 当我第一次运行它,它将工作大约10-12次,然后应用程序将崩溃与event.js抛出呃; 连接ETIMEDOUT

寻找更好的方法来请求https://cex.io/api/ticker/GHS/BTC中的 json数据。

所以我使用sailsjs,并在文件config / bootstrap.js中添加了我的服务来运行。

module.exports.bootstrap = function (cb) { // My tickerService.ticker(); // Runs the app cb(); }; 

这是我的一个尝试〜file api / services / tickerservice.js

 function storeTicker(){ console.log('Running!'); //retrieves info from https://cex.io/api/ticker/GHS/BTC require("cexapi").ticker('GHS/BTC', function(param){ console.log(param); Tickerchart.create( param, function tickerchartCreated (err, tickerchart) {}); }); } module.exports.ticker = function(){ setInterval(storeTicker, 6000); }; 

Cex.io库Github https://github.com/matveyco/cex.io-api-node.js/blob/master/cexapi.js

我使用模块的请求,并观察它的错误。 我也升级到风帆v0.10.x应用程序不会崩溃:D

 function storeTickerchart(){ //console.log('Running!'); var request = require("request"); var url = "https://cex.io/api/ticker/GHS/BTC"; request({ url: url, json: true }, function (error, response, body) { if (!error && response.statusCode === 200) { //console.log(body); //Print the json response Tickerchart.create(body, function tickerchartCreated (error, tickerchart) { if(error) console.log("Oops Error"); }); } }); } module.exports.ticker = function(){ setInterval(storeTickerchart, 5000); };