var Koa = require('koa');
var Router = require('koa-router' );
var bodyParser = require('koa-bodyparser');
var mysql = require('mysql');
var app = new Koa();
var router = new Router();
app.use(bodyParser());
var connection = mysql.createConnection({
host : 'localhost',
port:3306,
user : 'root',
password : 'xxx',
database : 'url'
});
connection.connect();
//第一类封装 ,一般用于不传递多个参数
const query = function (sql) {
return new Promise((resolve, reject) => {
connection.query(sql, function (error, results) {
if(error){
reject(error);
}else{
resolve(results)
}
});
})
}
//第二类封装 ,一般用于传递多个参数
const query1 = function (userStr,name,passwd,token1) {
return new Promise((resolve, reject) => {
connection.query(userStr, function (error, result) {
if(error){
reject(error);
}else{
if (result.length > 0) {
json['message'] = '用户已经存在';
json['resultCode']= 1;
} else {
json['message'] = '注册成功';
json['token'] = token1;
json['resultCode'] = 200;
var insertStr = `insert into login (username, password,token) values ("${name}", "${passwd}","${token1}")`;
console.log(insertStr)
connection.query(insertStr, function (err, res) {
if (err) throw err;
})
}
resolve(json)
}
});
})
}
。。。。。。。。。。。。。
作者:Vam的金豆之路
篇幅有限更多请见扩展链接:http://www.mark-to-win.com/tutorial/50814.html
版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除