title: Node JS Asynchronous Database Calls
body: <p>I am having issues getting node to make a database call without proceeding despite the database function has not returned a value. </p>
  
  <p>Here is the basic http server code: </p>
  
  <pre><code>var http = require('http');
  
  http.createServer(function (request, response) {
  
  response.writeHead(200, {
      'Content-Type': 'text/plain',
      'Access-Control-Allow-origin': '*' // implementation of CORS
  });
  
  response.end("ok");
  ;
  }).listen(8080,'0.0.0.0');
  </code></pre>
  
  <p>Using the request.on('data') function, I am able to decode JSON from requests and proceed that to make a database call:</p>
  
  <pre><code>request.on('data', function (chunk) {
      var json = JSON.parse(chunk);
      var id = parseInt(json["id"]);
      response.end(callDatabase(id));
  });
  </code></pre>
  
  <p>The database function goes something like this:</p>
  
  <pre><code>function callDatabase(id) {
      var result;
      var connection = mysql.createConnection(
          {
              host        :   '192.168.1.14',
              user        :   'root',
              password    :   '',
              database    :   'test'
          }
      );
  
      connection.connect();
      var queryString = 'SELECT name FROM test WHERE id = 1';
  
      connection.query(queryString, function(err, rows, fields) {
          if (err) throw err;
  
          for (var i in rows) {
              result = rows[i].name;
          }
      });
      connection.end();
      return result;
      }
  }
  </code></pre>
  
  <p>However under testing, this proves that I am doing it wrong. I am aware that I probably want to be using the node asynchronous module, which I have tired. I have also tried using the waterfall method, as well as parallel and many other tutorials online. I feel that the request.on function should be in parallel, then the database call async, so whilst node is waiting for the response from the database server, it is free to get on with any other requests, leaving the queued time to a minimum. </p>
  
  <p>Please inform me if I have miss-understood any of the concepts of node js. </p>