title: Show database content in browser with node.js, MySQL and Jade
body: <p>I'm currently working with Node.js, Express.js and Jade. My database is MySQL. I'm new to node.js, so I thought I try something very easy: Displaying some data from the database in a table in the browser.
  Unfortunately it still doesn't work. I can display data on an free port but not where I need it - on port 3000. And I also can't work with the response itself. This is one of the "solutions" or ideas I had. Maybe there is a problem with the asynchronous call? I simply have no idea.</p>
  
  <p>Here is my code:</p>
  
  <p>routes.js</p>
  
  <pre><code>var express = require('express'); 
  var controller = express.Router();
  var dataModel2 = require('../models/rooms');
  
  controller.get('/rooms', function(req, res, next) {
    var rooms = dataModel2();
    res.render('rooms', {
      items: rooms
    });
  });
  
  module.exports = controller;
  </code></pre>
  
  <p>models/rooms.js</p>
  
  <pre><code>var rooms;
  var connection = require('./databaseConnection');
  var http = require('http');
  
  rooms = function() {
    http.createServer(function (request, response) 
    { 
      console.log('Creating the http server');
      connection.query('SELECT * FROM rooms', function(err, rows, fields)
      {
        response.writeHead(200, { 'Content-Type': 'application/json'});
        var room = response.end(JSON.stringify(rows));
        return room;
      }); 
     });
  
  module.exports = rooms();
  </code></pre>
  
  <p>models/databaseConnection.js</p>
  
  <pre><code>var mysql = require('mysql');
  module.exports = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'raspi_key_royal'
  });
  </code></pre>
  
  <p>rooms.jade</p>
  
  <pre><code>extends layout
  
  block content
    div(id="bodyRoyal")
     table(border='1')
      thead
       tr
        th ID
        th Name
     tbody
      each item in items
       tr
        td=item.rid
        td=item.name
  </code></pre>
  
  <p>I splitted the functions a bit because there are some other sections like "persons" etc. I tried to insert console.logs in the rooms.js but that doesn't seem to work.
  I also thought I could save the response into a variable so that I can work with it somewhere else.</p>
  
  <p>Thank you for every help and hints!
  Steffi</p>