title: Node.js + Node-MySQL + Express 4 + Mustache
body: <p>I'm currently experimenting with Node.js, and I'm stuck a little bit. I'm trying to pass a query to Mustache.</p>
  
  <h3>Index.js</h3>
  
  <pre><code>//Load Express Framework
  
  var express = require('express');
  
  //Load Mustache Template Engine
  var mustachex = require('mustachex');
  
  //Load MySQL
  var mysql = require('mysql');
  var connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'nodeuser',
      password : 'testpass',
      database : 'node',
  });
  
  connection.connect(function(err) {
  });
  
  
  //Call express
  var app = express();
  
  //Set Global App Settings
  app.engine('html', mustachex.express);
  app.use(express.static(__dirname + '/public'));
  app.set('view engine', 'html');
  app.set('views', __dirname + '/views');
  
  //Call the layout
  app.get('/', function(req, res) {
      var sql    = 'SELECT * FROM menu WHERE id = 1';
      connection.query(sql, function(err, results) {
      });
      for (var i in results){
           var sqlResult = results[i];
           console.log(sqlResult['address'], sqlResult['text']);
      }
      res.render('index');
  });
  
  //app.get('/test', function(req, res) {
  //  res.render('test');
  //});
  
  
  //Create Server
  var port = Number(process.env.PORT || 5000);
  app.listen(port, function() {
      console.log("Listening on " + port);
  });
  </code></pre>
  
  <h3>Header.html (partial)</h3>
  
  <p>In here I'm trying to load the queried data from MySQL.</p>
  
  <pre><code>&lt;div id="menu-container"&gt;
      &lt;div id="menu"&gt;
          &lt;span class="text"&gt;&lt;a href="{{address}}"&gt;{{text}}&lt;/a&gt;&lt;/span&gt;
          &lt;span class="text"&gt;&lt;a href="/"&gt;Home&lt;/a&gt;&lt;/span&gt;
           &lt;span class="text"&gt;&lt;a href="/services"&gt;Services&lt;/a&gt;&lt;/span&gt;
          &lt;span class="text"&gt;&lt;a href="/contact"&gt;Contact&lt;/a&gt;&lt;/span&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I just started learning Node.js two days ago. I was able to figure out how to use the partials with Mustache, but I can't figure out MySQL. I already created the necessary tables in the node database.</p>
  
  <h3>MySQL</h3>
  
  <pre><code> menu
   -&gt; ID (INT) -&gt; 1
   -&gt; Address (Text) -&gt; www.example.com
   -&gt; Text (Text) -&gt; Test
  </code></pre>
  
  <p>How can I fix this problem?</p>
  
  <p>It's quite hard to find good Node.js manuals.</p>