Vue--- 一点车项目 连接数据库
创建连接数据库配置
###导入
const Koa = require('koa');
const Router = require('koa-router');
const mysql = require('mysql');
const co = require('co-mysql'); ###创建
let server = new Koa();
let router = new Router(); server.listen(8081);
server.use(async (ctx,next)=>{
ctx.set('Access-Control-Allow-Origin', '*'); await next();
}); ###数据库 创建配置项
let conn = mysql.createPool({
host:'localhost',
user:'root',
password:'',
database:'houdunwang'
});
###放入 co中
server.context.db = co(conn); ###服务器选择主路由
server.use(router.routes()) #####路由路径,执行什么样的 sql语句
// 这些都是 省
router.get('/sheng',async ctx=>{
let data = await ctx.db.query('SELECT * FROM city WHERE type = 1');
ctx.body = data;
});
// 这些 都是市区
router.get('/city/:pid',async ctx=>{
let {pid} = ctx.params;
let data = await ctx.db.query('SELECT * FROM city WHERE pid=?',pid);
ctx.body = data;
});