title: How to organize communication with database in Node.Js?
body: <p>I have a problem to design communication with MySQL database in my Nodejs's app.
  The biggest problem is that queries are async, so it becomes complicated to design my projects. For example, I have excercises.js </p>
  
  <p><strong>EXCERCISES.JS</strong></p>
  
  <pre><code>var express = require('express');
  var database = require('../database/database.js');
  
  var router = express.Router();
  
  console.log(database.db(saveDbData))
  /* GET users listing. */
  router.get('/', function(req, res, next) {
  
    res.render('exercises',{title: 'Exercises', ex: #DATABASE RESULT});
  });
  
  module.exports = router;
  </code></pre>
  
  <p>In need to write query's result in <strong>ex</strong> field.</p>
  
  <p>Then I write a module to handle mysql connection</p>
  
  <p><strong><em>DATABASE.JS</em></strong></p>
  
  <pre><code>var pool = mysql.createPool({
      connectionLimit: 10000,
      host: 'localhost',
      user: 'root',
      password: 'password',
      database: 'Example'
  });
  
  var results;
  
  var db = function(){
      pool.query('SELECT name FROM Exercises', function(error, results, fields){
      if (error) throw error;
      res = results;
  })
      return res;
  }
  
  module.exports = {
      db: db,
  }
  </code></pre>
  
  <p>Obviously, It doesn't work because pool.query is async.
  The only alternative that I've found on the web is something like this:</p>
  
  <p>EXERCISES.JS</p>
  
  <pre><code>
  var pool = mysql.createPool({
      connectionLimit: 10000,
      host: 'localhost',
      user: 'root',
      password: 'password',
      database: 'Example'
  });
  
  
  pool.query('SELECT name FROM Exercises', function(error, results, fields){
      if (error) throw error;
      router.get('/', function(req, res, next) {
          res.render('exercises',{title: 'Exercises', ex: result[0].name});
      }); 
  })
  
  </code></pre>
  
  <p>But in this way, mysql parts and routing/render parts are mixed. Is it still a well-designed solution? Are there more elegant solutions?</p>
  
  <p><strong><em>EDIT</em></strong>:</p>
  
  <p>I have modified the files and I have used Promise like this</p>
  
  <p><strong>EXERCISES.JS</strong></p>
  
  <pre><code>var express = require('express');
  var database = require('../database/database.js');
  
  var router = express.Router();
  
  
  var data = database.db()
  .then(
      function(data){
          console.log("Resolved");
          /* GET users listing. */
          router.get('/', function(req, res, next) {
              res.render('exercises',{title: 'Exercises', ex: data[0].name});
          });
      })
  .catch(
      error =&gt; console.error(error));
  
  
  
  module.exports = router;
  
  </code></pre>
  
  <p><strong><em>DATABASE.JS</em></strong></p>
  
  <pre><code>ar mysql = require('mysql');
  
  
  var pool = mysql.createPool({
      connectionLimit: 10000,
      host: 'localhost',
      user: 'root',
      password: 'password',
      database: 'Example'
  });
  
  var res;
  var db = function(){
      return new Promise(function(resolve, reject){
          pool.query('SELECT name FROM Exercises', function(error, results, fields){
              if (error) reject(error);
              resolve(results)
  
          })
      })
  }
  
  
  module.exports = {
      db: db,
  }
  </code></pre>
  
  <p>And it works, but I don't think it is the best solution. For example, what if I want to get data for render from more queries? 
  I'm new in these technologies so I'm not able to figure out the best way to integrate database and html pages's rendering.</p>