自己写的一段demo…
// db.js
var mysql = require('mysql');
module.exports = function () {
var pool = mysql.createPool({
host: "localhost",
user: 'xxx',
password: 'xxx',
database: 'xxx',
});
return pool;
}();
// valid.js
var pool = require('./db.js');
module.exports = function () {
return {
isProjectOwner: function (uid, pid, callback) {
pool.getConnection(function (err, connection) {
if (err) {
callback(err, null);
}
var sql = '.....';
connection.query(sql, [pid, uid], function (err, res, fields) {
if (err) {
callback(null, false); // Maybe attack
}
console.log(res);
if (res.length) {
callback(null, true);
} else {
callback(null, false);
}
connection.release();
});
});
},
};
}();
// tdb.js
var valid = require('./valid.js');
valid.isProjectOwner(2, 1, function (err, result) {
if (err) {
throw err;
}
console.log(result);
});
在Terminal中运行tdb.js
> $ node tdb.js
[]
false
但是,不使用Ctrl+C无法结束进程。。
求教各位大大,谢谢~