title: How to execute MySQL queries in node.js views?
body: <p>I have imported <code>mysql</code> module in my <code>app.js</code>:</p>
  
  <pre><code>var mysql = require('mysql');
  </code></pre>
  
  <p>Then I set <code>mysql</code> configuration:</p>
  
  <pre><code>// Database configuration
  
  var HOST            = 'localhost';
  var PORT            = 3306;
  var MYSQL_USER      = 'root';
  var MYSQL_PASS      = 'pass';
  var DATABASE        = 'ruchevits_website';
  </code></pre>
  
  <p>Created <code>mysql</code> client object:</p>
  
  <pre><code>// Create database client
  
  var db = mysql.createClient({
      host:       HOST,
      port:       PORT,
      user:       MYSQL_USER,
      password:   MYSQL_PASS
  });
  </code></pre>
  
  <p>And selected my database:</p>
  
  <pre><code>// Use database
  
  db.query('use ' + DATABASE);
  </code></pre>
  
  <p>So, now I can execute queries like:</p>
  
  <pre><code>db.query('select id, firstname, lastname from mytable', function(err, result, fields){
      if (err)
      {
          throw err;
      }
      else
      {
          console.log('Selected people:');
          for (var i in result)
          {
              var people = result[i];
              console.log(people.firstname + ' ' + people.lastname);
          }
      }
  });
  </code></pre>
  
  <p>But it is inconvenient to execute all queries in <code>app.js</code>.</p>
  
  <p>I want to execute them in specific <code>routes</code> files in the section:</p>
  
  <pre><code>exports.index = function(req, res)
  {
      // MySQL query
  
      res.render('resume', {
          title: 'Resume'
      });
  };
  </code></pre>
  
  <p>What is the correct way to do that?</p>
  
  <p><em>I need to use <code>db</code> object not in <code>app.js</code> file. Actually, the question is how to pass it?</em></p>