简体   繁体   English

使用Node.js和mysql表实现Jade

[英]Implementing Jade with Node.js and mysql table

I am attempting to take items on my 'users' table and format them into a table using Jade to display for my web app. 我试图将项目放在“用户”表上,并使用Jade将其格式化为表格以显示在我的Web应用程序中。 I am unsure on how to send information from 'users' to Jade in order to create the list and I am unsure about the corresponding code necessary to implement the table using Jade. 我不确定如何将信息从“用户”发送到Jade以创建列表,也不确定使用Jade实现表所需的相应代码。 Below is my code so far in Node.js. 下面是到目前为止我在Node.js中的代码。 Any advice is appreciated! 任何建议表示赞赏!

var pub = __dirname;
var express = require('express');
var app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(app.router);
app.use(express.static(path.join(pub,'public')));


var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static('static_files'));

var mysql = require('mysql');

var pool = mysql.createPool({
 host: 'localhost',
 port:'3306',
 user: 'root',
 password: '9696',
 database: 'users'
 });



app.get('/list', function(req, res){
pool.getConnection(function(err,con){
con.query('SELECT * FROM user set' , function(err, results){
if (err) {
  throw err;
}
res.render('username', {
  title: results[0].title,
  results: results
});
});
});
});

Part 1 - How to pass variables to the jade processor (you did this correctly): 第1部分 -如何将变量传递给玉器(您正确地做到了):

  res.render('username', {
      title: results[0].title,
      results: results
  });

Part 2 - How to use the variables in the jade page: 第2部分 -如何在Jade页面中使用变量:

First of all, remember that you are passing a single variable object to the jade processor that contains all the variable you will use. 首先,请记住,您正在将单个变量对象传递给包含所有将要使用的变量的玉器。 However, the properties are referred to by simply calling them (so title === results[0].title ). 但是,仅通过调用属性即可引用属性(因此title === results[0].title )。

Jade has 2 formats for inserting variables depending on the situation: Jade根据情况插入变量有2种格式:

  1. The regular method of: 常规方法:

      title My Website | #{title} // #{nameOfPropertyOfPassedVariableObject} 
  2. When dealing with quotes such as li a(href="/myusers/#{user.email}") , you have to do it slightly differently: 处理诸如li a(href="/myusers/#{user.email}") ,您必须做些不同:

      li a(href="/myusers/"+user.email) // +nameofpropertyofpassedvariableobject 

    with no #{} but with a + and also the variable must be outside the quotes . 没有#{}但带有+ ,并且变量必须在引号之外

    This is just one of the funky things with jade. 这只是翡翠的时髦事物之一。

Let me know if you also want to know about loops and stuff. 如果您也想了解循环和内容,请告诉我。 Also, here are 2 excellent resources for jade (these are how I started): 另外,这里有2个关于翡翠的极好的资源(这些是我的入门方式):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM