节点js无限循环 [英] Node js infinite loop

查看:78
本文介绍了节点js无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点服务器,它从php服务器中提取信息,并通过回调将其发送到需要的地方.广告一天或两次,服务器陷入无限循环,这是因为我没有正确处理php和节点服务器之间的连接.我在下面发布了我的身份验证代码.有人有什么想法吗?

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");
                    }
            });
    });

};

推荐答案

您确定要对功能进行身份验证吗?您的代码似乎是完美的.有两件事要做

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

1.对请求错误,请求超时和请求中止进行回调.

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

2.深入了解您的回调,在下面的代码中,您在引发异常时调用相同的函数.你确定吗?.如果再次达到身份验证"功能,则可能导致循环.

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");
}

这篇关于节点js无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆