如何在 CoffeeScript 中连接 MySQL 数据库。如何运行它。我使用 node.js 创建了 MySQL 连接并在终端中显示记录,但如何在浏览器上显示它。我正在使用 ECO 模板在浏览器上显示。我需要使用咖啡脚本进行数据库连接
文件 - db_test.js
var mysql = require("mysql");
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
});
con.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
con.query('SELECT * FROM users',function(err,rows){
if(err) throw err;
for (var i = 0; i < rows.length; i++) {
console.log(rows[i].username);
};
});
con.end(function(err) {
// The connection is terminated gracefully
// Ensures all previously enqueued queries are still
// before sending a COM_QUIT packet to the MySQL server.
});