我是 node.js 和 express 框架的新手。
谁能告诉我我这样做是否正确?
我创建了一个 database.js 作为模块,代码包含:
var mysql = require('mysql'),
dateFormat = require('dateformat'),
db = require('./dashboard');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'selenium',
timezone: '-07:00'
});
exports.selectalldate = function() {
connection.query('SELECT * FROM date', function (err, rows, fields) {
if (err) {
console.log(err);
}
if(rows.length > 0) {
for(i = 0; i < rows.length; i ++) {
rows[i].date = dateFormat(rows[i].date, "yyyy-mm-dd")
}
return rows;
} else {
return false;
}
});
}
我在 app.js 中需要它,当我调用 selectalldate() 以从 app.js 中的数据库中获取所有日期并查看结果时。我不确定。我在这里做错了什么?
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, fs = require('fs')
, file = __dirname + '/test2'
, get = require('./routes/get')
, db = require('./routes/database')
;
app.get('/dashboard', function(req, res) {
var datee = db.selectalldate();
console.log(datee);
res.render('dashboard', {title: 'Selenium Dashboard', date: datee});
});