https://npm.taobao.org/package/mysql-pro
server-mysqlpro.js
const koa = require('koa');
const router = require('koa-router');
const DB = require('mysql-pro');
let db = new DB({
mysql: {
host:'127.0.0.1',
port:'3306',
database: '20180211',
user:'root',
password:'admin'
}
})
let server = new koa();
server.listen(8080);
let r1 = router();
server.use(r1.routes());
r1.get('/user',async ctx=>{
try{
await db.startTransaction();
let data = await db.executeTransaction(`select * from user_table`,[]);
await db.stopTransaction();
console.log(data);
ctx.response.body = data;
}catch(e){
ctx.response.body = '数据库获取异常';
}
})
image.png
网友评论