node.js操作数据库模块化
const mysql = require('mysql');
const con = mysql.createConnection({
host:'localhost',
user:'root',
password:'root',
database:'xiaouclass',
port:3306
})
con.connect();
// promise封装
function myQuery(sql){
let p = new Promise((resolve,reject)=>{
con.query( sql ,(err,result)=>{
resolve([err,result])
})
})
return p;
}
// 暴露
module.exports = myQuery;
// 调用myQuery()
/* async function fn(){
let s = await myQuery('select * from student limit 2');
console.log(s)
}
fn()*/