最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show database content in browser with node.js, MySQL and Jade - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question asked Jul 7, 2015 at 8:40 Stefanie WeberStefanie Weber 671 gold badge2 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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

});

This is from the express site...http://expressjs./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);

});
发布评论

评论列表(0)

  1. 暂无评论