返回值,或停止脚本

时间:2014-03-07 10:37:35

标签: node.js azure azure-mobile-services

直升机, 我在Windows Azure Mobile服务中创建API,在这个api脚本中,我有连接其他服务的功能。当我从服务中得到很好的答案时,我有问题如何返回值或停止可执行的脚本。函数process.exit(1),不起作用。

    function function1(item,response) {
    var buf ='';
    var net = require('net');
    var HOST = 'xxxx.xxxx.xxxx.xxxx';
    var PORT = xxx;

    var client = new net.Socket();

    client.setTimeout(100000, function() {
        console.log("Timeout");
        response.send(500, "Timeout");
    });

    client.connect(PORT, HOST, function() {
        client.write(item + "\n");
        client.on('data', function(data) {
            buf = buf + data.toString('utf-8');         
        });

        client.on('close', function() {
        });

        client.on('end', function() {
            if (buf.length > 1) {    
                    var result = JSON.parse(buf);
                    //if resulr.Aviable is true the functios should return result or     send result and stop execiuting script
                    if ( result.Avaiable) {
                         response.send(200, result);
                         //now i wont't to respond answer to client or return my value(result)
                         console.log('Send data');                         
                    }
            }           
             client.destroy();             
        });                                      
    });  

    }

1 个答案:

答案 0 :(得分:0)

另一种方法是使用一个标志来指示是否已发送响应。这样,当达到第一个备选方案时,您可以将标志设置为true(可能清除超时,因此它不会超出其需要),并且在所有情况下都会在返回响应之前检查标志是否已设置。以下代码的内容:

function function1(item,response) {
    var buf = '';
    var net = require('net');
    var HOST = 'xxxx.xxxx.xxxx.xxxx';
    var PORT = xxx;

    var client = new net.Socket();

    var responseSent = false;

    var timeoutHandler = client.setTimeout(100000, function() {
        if (!responseSent) {
            responseSent = true;
            console.log("Timeout");
            response.send(500, { error: "Timeout" });
        }
    });

    client.connect(PORT, HOST, function() {
        client.write(item + "\n");
        client.on('data', function(data) {
            buf = buf + data.toString('utf-8');         
        });

        client.on('close', function(had_error) {
            if (!responseSent) {
                clearTimeout(timeoutHandler);
                responseSent = true;
                console.log('Socket closed');
                response.send(500, { error: had_error ? 'Socket error' : 'unknown' });
            }
        });

        client.on('end', function() {
            if (!responseSent) {
                responseSent = true;
                clearTimeout(timeoutHandler);
                if (buf.length > 1) {
                    try {
                        var result = JSON.parse(buf);
                        if (result.Available) {
                            response.send(200, result);
                        } else {
                            response.send(500, { error: 'Socket data is not available' });
                        }
                    } catch (ex) {
                        response.send(500, { error: 'error parsing JSON', exception: ex });
                    }
                } else {
                    // We should always return a response
                    response.send(500, { error: 'No data read from socket' });
                }
            }
            client.destroy();             
        });
    });
}

请注意,由于node.js在单个线程上运行,因此您可以假设不会发送响应两次的情况。此外,您应该确保响应始终发送一次 - 在您拥有的代码中,如果存在套接字错误,或者buf.length不大于1,或者result.Avaiable不是真的,然后发送超时响应,但您不需要等待整个(100秒)时间发送该响应。