0

I have a node server that pulls info from a php server and sends it over where it needs to be through a callback. Once or twice ad day the server gets stuck in an infinite loop and I think it's because I'm not properly handling the connection between the the php and node server. I posted my authentication code below. Anyone have any ideas?

exports.authenticate = function(request, callback) {
   var https = require('https');

   var options = {
            hostname: 'www.mysite.com',
            port: 443,
            path: '/site/chatauth?id=' + request.sessionID,
            method: 'GET',
    };
    var req = https.request(options, function(res) {
        //console.log("statusCode: ", res.statusCode);
        // console.log("headers: ", res.headers);

         res.on('data', function(d) {
         // process.stdout.write(d);
         });
        });

    req.end();
    req.on('response', function (response) {
            var data = '';
            response.setEncoding('utf8');
            response.on('data', function(chunk) {
                    data += chunk;
            });

           // console.log (request.sessionID);

            response.on('end', function() {
                    try {
                            callback(JSON.parse(data));
                    } catch(e) {
            callback(); 
                            console.log("authentication failed");
                    }
            });
    });

};

3
  • 1
    What does infinite loop mean? It logs the same lines over in repetition? Commented Nov 2, 2013 at 5:50
  • Yup, it logs the same lines over. I have another middleware file that will update the listeners and it keeps hitting that line and brings the CPU useage to 99%. I think there is something in this code that is not allowing node to return to the event loop. Commented Nov 2, 2013 at 18:27
  • What lines, where it gets stuck? Commented Nov 2, 2013 at 20:10

2 Answers 2

0

are you sure authenticate function? Your code is seems to be perfect. There is two thing you have to do

1.Make callback on request error, request timeout and request abort.

2.Get deep insight about your callback, In following code you call same function on throwing exception. Are you sure about this?. This could make possibly loop, if it is again reach "authentication" function.

try {
    callback(JSON.parse(data));
} catch(e) {
    callback();
    console.log("authentication failed");
}
Sign up to request clarification or add additional context in comments.

Comments

0

I can't find any errors in your code, but due to emitter/asynchronous way perhaps I didn't catch it completely.

I personally use npm module request (Simplified HTTP request client) w/o any problems so far. You could easily give it a try:

var request = require('request');
request({
    uri: 'https://www.mysite.com/site/chatauth?id=' + request.sessionID,
    encoding: 'utf8',
    timeout: 20000,
    strictSSL: true
}, function (err, response, body) {
   if (err || response.statusCode !== 200) {
       // some error handling
       callback('error');
       return;
   }
   callback(null, JSON.parse(body));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.