Event-oriented programming

I'm trying to transfer the server part to use for my project node.js. There was a problem that I can't solve - I don't understand the logic of the construction that needs to be built. Details in the code:

var httpServer = http.createServer(function (request, response) {
var parseUrl=url.parse(request.url,true),
    pathname = parseUrl.pathname,
    obj=parseUrl.query;
//console.log(parseUrl.query);
var answer="ERROR";
if(pathname=="/getdata") {
    if(obj.name != undefined && obj.pwd != undefined && obj.com != undefined) {
        answer=makeRequest(obj.name,obj.pwd,obj.com);
    } else {
        answer="error in data";
    }
} else {

}
response.writeHead(200, {"Content-Type": "text/html", 'Access-Control-Allow-Origin' : '*'});
response.write(answer);
response.end();
});

Here, the server processes the client request, but to respond to it, the server needs to forward the data to the socket of another server, get the data, and return it back to return to the client. from makeRequest(...) I want to get a response from a socket that is communicating with so far, it is implemented as follows:

function makeRequest(name,pwd,com) {

var client = new net.Socket(),answer=[],flag=0;
client.connect(3201, "127.0.0.1", function() {
    client.write('auth "'+name+'" "'+pwd+'"\n');
});

client.on('data', function(data) {
     answer.push(data);
         if(data=='_bye') client.destroy();  
});

client.write(com+'\n');

client.on('close', function() {
    return answer; // вот это работать не будет. а мне нужно при закрытии сокетного
                   // соединения вернуть последний ответ в функцию, которая вызвала
                   // этот makeRequest(), т.е. как-то обернуть или замкнуть...
});

}

Please help me correctly create such a construction - return data from the socket, which in turn should return the response to the request to the node server.js

Author: Nicolas Chabanovsky, 2012-11-26

3 answers

Pass to makeRequest the callback that will be called when the connection is closed

// ...
makeRequest(obj.name, obj.pwd, obj.com, function(answer) {
    response.write(answer);
})
// ...
// ...
function makeRequest(name, pwd, com, onClose) {
    // ...
    client.on('close', function() {
        onClose(answer)
    });   
}

Well, the rest of the code will need to be corrected a little.

 3
Author: gkuznets, 2012-11-27 02:26:23

answer must handle callback, which makeRequest must accept in parameters, i.e. something like:

function makeRequest( bla, bla, bla, callback ) {
  // .....
  client.on('close', function(){ callback( answer ) })
}
if( obj.name && obj.pwd && obj.com ) {
  makeRequest( bla, bla, bla, handleAnswer)
}
else {
  handleAnswer( 'error in data' );
}

function handlerAnswer( answer ) {
  // check answer here
}
 2
Author: Deleted, 2012-11-27 15:11:37

This is your nothing EventDriven approach. This is the usual procedural approach, in which you write some crutches.

The classical Event Driven approach is usually built around an event loop and the exchange of messages/events between procedures and / or objects.

You don't have a processing loop or a dispatcher and have something similar to messages. Rework the code in the direction of the appearance of the processing cycle - then everything will become easier and clearer.

 0
Author: Barmaley, 2012-11-27 15:12:37