1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| // router/index.js
const Router = require('koa-router')
// const User = require('../db/user') const home = new Router()
home.get('/', async (ctx) => { let title = '首页' await ctx.render('index', { title }) })
// 子路由2 const page = new Router()
page.get('/404', async (ctx) => { let title = "404" await ctx.render('err', { title }) })
const login = new Router()
login.get('/', async (ctx) => { let title = "登录" await ctx.render('login', { title }) }).post('/', async (ctx) => { const data = ctx.request.body let queryres = await User.queryEmail(data.email) console.log(queryres) if (queryres) { if(queryres[0].password === data.password) { ctx.body = { 'code': 1, 'data': queryres[0], 'mesg': '登录成功' } }else { ctx.body = { 'code': 0, 'data': {}, 'mesg': '密码错误' } } }else { ctx.body = { 'code': 0, 'data': {}, 'mesg': '没有该用户,去注册吧' } } })
const register = new Router()
register.get('/', async (ctx) => { let title = "注册" await ctx.render('register', { title }) }).post('/', async (ctx) => { const data = ctx.request.body let queryres = await User.queryEmail(data.email) if (queryres) { ctx.body = { 'code': 0, 'data': {}, 'mesg': '该邮箱已经存在哦' } }else { await User.save(data) ctx.body = { 'code': 1, 'data': {}, 'mesg': '保存成功' } } })
// 装载所有子路由 let router = new Router() router.use('/', home.routes(), home.allowedMethods()) router.use('/page', page.routes(), page.allowedMethods()) router.use('/login', login.routes(), login.allowedMethods()) router.use('/register', register.routes(), register.allowedMethods())
module.exports = router
|