Stefanie Weber
Stefanie Weber

Reputation: 67

Show database content in browser with node.js, MySQL and Jade

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.

Here is my code:

routes.js

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;

models/rooms.js

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();

models/databaseConnection.js

var mysql = require('mysql');
module.exports = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'raspi_key_royal'
});

rooms.jade

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

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.

Thank you for every help and hints! Steffi

Upvotes: 2

Views: 14203

Answers (2)

michelem
michelem

Reputation: 14590

Something like this should do it:

var express = require('express'),
    app = express(),
    connection = require('./databaseConnection');

app.get('/rooms', function (req, res) {
    connection.query('SELECT * FROM rooms', function(err, rows, fields)
    {
        res.render('rooms', {
          items: rows
        });
    });
});

var server = app.listen(3000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

Upvotes: 2

Will
Will

Reputation: 773

This is from the express site...http://expressjs.com/starter/hello-world.html

app.listen(3000, ...

is the how to configure a it to a specific port (in this case 3000).

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var server = app.listen(3000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

Upvotes: 0

Related Questions