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) } }); }) } //
使用第一类封装 router.get( '/csdnurl', async(ctx, next) => { var sql = "select * from
csdnurl"; let results = await query(sql); ctx.body=results }); // 使用第一类封装 router
.post('/login', async (ctx, next) => { var name = ctx.request.body.username; var
passwd= ctx.request.body.password; var userStr = `select
username,password,token from login where username="${name}" and password="${
passwd}"`; let results = await query(userStr); ctx.body = results }); // 使用第二类封装
router.post('/register', async (ctx, next) => { var name = ctx.request.body.
username; var passwd = ctx.request.body.password; var token1 = jwt.sign({
username: name }, secretkey, { expiresIn: 60 * 8 }); var userStr = `select *
from login where username="${name}"`; let results = await query1(userStr,name,
passwd,token1); ctx.body = json });
这里主要使用了 async、await 处理异步问题

技术
©2020 ioDraw All rights reserved
OpenCV之IplImage详解央行:微信支付宝和数字人民币不存在竞争关系vue el-input 禁止输入特殊字符 只可输入数字 正则验证今天你抱怨了吗?分享一场黑客帝国代码雨脚本,简单好玩!详解PHP中的die、exit、return天猫等网店要挟用户成风?要硬钢!别惯他们臭毛病使用函数实现两个数的交换(C语言)django不关闭CSRF中间件,自定义通过CSRF检测的post请求python网络编程简单记录