0

I´m writing a Node.js app which should make an HTTP request using "request" module and save the response in Parse with some parameters and so. I use setInterval() for the loop.

Problem is I'm getting always the same response, like it´s cached or something. If I do a cURL form my local machine I see the actual data, however the loop in Node.js seems to get always the same response.

EDIT with code:

//Loop
setInterval(function(){
    try {
        foo.make_request();
    }catch(e){
        console.log(e);
    }
}, 30 * 1000); //30 secs

and my make_request function:

function _make_request(){
    //Configure the request
    var options = {
        url: 'http://player.rockfm.fm/rdsrock.php',
        method: 'GET'
    };

    //Start the request
    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            // Print out the response body
            var artist = body.substring(0, body.indexOf(':'));
            var title  = body.substring(body.indexOf(':')+3, body.indexOf('@')-1);
            console.log(artist + " - " + title);
            //upload to Parse etc etc
        }    
    });
}

module.exports.make_request = _make_request;
alejandromav
  • 933
  • 1
  • 11
  • 24
  • Your code looks fine. As far as I understand, this `url` returns the current playing song, and your code makes a request every 30 seconds. So, unless the current song has finished playing, you won't get back different data. – Rodrigo Medeiros Mar 23 '15 at 11:37
  • Yes, that's it. But the songs changes, and i see the changes with cURL, for example. But not form Node, it seems it caches the response after 10 times or something. – alejandromav Mar 23 '15 at 11:50
  • Well, it works for me. I left the app running for some minutes and it's returning the same data that a `curl` request returns. – Rodrigo Medeiros Mar 23 '15 at 11:56
  • Yeah, it should work. But it starts failing after 10 attemptsor so. I've read somewhere "http" module has 5 open connections limit. Maybe "request" module has some kind of limit like this. I'll try adding "response.on('data', function() {});" to my make request function, so I consume my response and I avoid hitting this limit. Just guessing... - http://stackoverflow.com/questions/16965582/node-js-http-get-hangs-after-5-requests-to-remote-site – alejandromav Mar 23 '15 at 12:16

1 Answers1

0

Yeah! I left it working all the afternoon and it worked well:

request(options, function (error, response, body) {
    response.on('data', function() {});
    if (!error && response.statusCode == 200) {
        // Print out the response body
        var artist = body.substring(0, body.indexOf(':'));
        var title  = body.substring(body.indexOf(':')+3, body.indexOf('@')-1);
        console.log(artist + " - " + title);
        //upload to Parse etc etc
    }    
});

The solution was to actually consume the response with .on() method. As it turns out, you need to do it when throwing many request at the same time.

alejandromav
  • 933
  • 1
  • 11
  • 24