What is the best way to get JSON from a URL in Node.js

So, I use sailsjs to request json data from an external website and put that data in the creation path. When I first launch it, it will work about 10-12 times, and then the application will crash using event.js throw er; connect ETIMEDOUT

Looking for the best way to request json data from https://cex.io/api/ticker/GHS/BTC .

So, I use sailsjs in the config / bootstrap.js file as well. I added my service to run.

module.exports.bootstrap = function (cb) {

    // My
    tickerService.ticker();

    // Runs the app
    cb();
};

This is one of my attempts ~ 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 library https://github.com/matveyco/cex.io-api-node.js/blob/master/cexapi.js

+4
1

. sails 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);

};
+1

Source: https://habr.com/ru/post/1541919/


All Articles