javascript 使用 node.js、MySQL 和 Jade 在浏览器中显示数据库内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31263721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:33:34  来源:igfitidea点击:

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

javascriptmysqlnode.jsexpresspug

提问by Stefanie Weber

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.

我目前正在使用 Node.js、Express.js 和 Jade。我的数据库是 MySQL。我是 node.js 的新手,所以我想我尝试了一些非常简单的方法:在浏览器的表格中显示数据库中的一些数据。不幸的是它仍然不起作用。我可以在空闲端口上显示数据,但不能在我需要的地方显示数据 - 在端口 3000 上。而且我也无法使用响应本身。这是我的“解决方案”或想法之一。也许异步调用有问题?我只是不知道。

Here is my code:

这是我的代码:

routes.js

路由.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

模型/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

模型/数据库连接.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.

我对功能进行了一些拆分,因为还有一些其他部分,例如“人员”等。我尝试在rooms.js 中插入console.logs,但这似乎不起作用。我还认为我可以将响应保存到一个变量中,以便我可以在其他地方使用它。

Thank you for every help and hints! Steffi

感谢您的每一个帮助和提示!史蒂菲

采纳答案by michelem

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

});

回答by Will

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

这是来自快递网站... 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).

是如何将它配置到特定端口(在本例中为 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);

});