Sunday, 4 June 2017

NodeJS request module stop working after 6 ajax requests

I have in node rest api function that receive ajax call with id to restart url link....it works when i call this ajax function 5 times...after i call 6 time it is not working..using console.log i see that line stops on getting request and if i call it again i get same log...getting request from url (url is valid)...so i see that request module is not returning any error even or request response code...here is my code:

apiRoutes.post('/restart_stream', function(req, res) {
/* GET - token */
var token = req.headers['x-access-token'];
    token = decrypt(token);
    console.log('called.../restart_stream');

/* GET - data id */
var data     = {};
data.id      = req.body.id;
data.type    = req.body.type;

    /* GET - connection */
    pool.getConnection(function(err,connection){
        if (err) {
            connection.release();
            res.json({"code" : 100, "status" : "Error in connection database"});
            return;
        }

        /* GET - stream url */
        connection.query("SELECT id, channel, JSON_UNQUOTE(json_extract(stream, '$[0]')) AS stream_url FROM streams WHERE id=?", [data.id],function(err, rows, fields){
       console.log('getting request to...'+rows[0].stream_url);
            /* GET - stream status */
            var r = request
                .get(rows[0].stream_url)
                .on('response', function(response) {
                    console.log('resoponse received....'+response.statusCode);
                    /* CHECK - stream exists */
                    if (response.statusCode != 200 && response.statusCode != 302) {
                        r.end();
                        /* RESET - live&streams info */
                        res.json({"status": "false", "message": "FAILED to Restart <b>"+rows[0].channel+"</b>!"});
                    } else {
                        /* RESET - live&streams info */
                        connection.query("UPDATE streams SET uptime=?, status=? WHERE id=?",['0', '1', data.id], function(err) {
                            /* CHECK - error */
                            if (err) {
                                res.json({"status": "false", "message": err.message});
                            } else {
                                /* STOP - url my code here that is not executed after 6 ajax call */


                                r.end();

                                res.json({"status": "true", "message": "RESTARTED Successfully!"});
                            };
                        });
                    };
                })
        });
    });
} else {
    res.json({"status": "false", "message": "FAILED"});
};
});

I try using r.end() so that request connection is closed after each response but request module stop working after 5th ajax call..any clue what i im working wrong?

This is node request module that connects to url and get response code:

https://www.npmjs.com/package/request



via John

No comments:

Post a Comment