@koa/router使用回顾用户路由定义koa中间件使用Postman基础测试工作内容后台:参数采集后端:数据库写入和查询Postman:接口测试准备npmi-Skoa-body//处理业务逻辑参数获取,用于Post、Put、Patch请求参数,支持加载存储到ctx.request.body//更新文件:/服务器/app.jsconstkoa=require('koa');constbodyParser=require('koa-body');//添加新的constroutes=require('./router');constapp=newkoa();app.use(bodyParser());//新增:在中间件处理请求数据之前调用//app.use((ctx,next)=>{//ctx.body='testtesttest';//next();//})//删除无用代码app.use(...router.routes).use(...router.allowedMethods);routes.forEach(route=>{app.使用(路由);});app.on('error',err=>{log.error('服务器错误',err)});module.exports=app;更新注册&登录路由调用POST方法分别访问/users?action='register'和/users?action='login'。获取url附加的参数?通过ctx.query。//更新文件:/server/router/users.js部分代码...{path:'/',method:'POST',handle:async(ctx)=>{const{action}=ctx.query;switch(action){case'register':awaitregister(ctx);休息;案例'登录':等待登录(ctx);休息;默认值:等待列表(ctx);点击查看数据注册逻辑更新注册逻辑//更新文件:/server/control/user.jsconstuserModel=require('../model/user');...asyncfunctionregister(ctx){const{account,password}=ctx.request.body;//获取payloadif(!account||!password){ctx.body={//返回jsoncode:'403',data:null,msg:'帐号/密码不能被空'}返回;}try{constuser=awaituserModel.findOne({//检查数据库是否已有数据账号});if(user){ctx.body={code:'403',data:null,msg:'Useralreadyexists'}}else{constnewUser=awaitnewuserModel({//新数据账号,密码})。节省();ctx.body={code:'200',data:newUser,msg:'注册成功'}}}catch(err){ctx.body={code:'403',data:null,msg:err.message}}}...通过ctx.request.body获取调用POST方法传递的参数。使用userModel.findOne(