1.安装koa生成器npminstallkoa-generate-g#查看是否安装成功kao2-V2.创建工程//命令koa2[name][-e]e表示使用ejsenginekoa2hello-e3.安装运行#进入项目cdhello#安装依赖npminstall#运行npmstart4.使用token服务器配置constJwt=require('koa-jwt');constJsonwebtoken=require('jsonwebtoken');//使用jwtapp.use(Jwt({secret:'s'}).unless({//过滤不需要认证的路由path:[/^\/public\/login/...]}))//获取tokenapp.use(async(ctx,next)=>{//token解密,获取用户信息lettoken=ctx.headers.authorizationletuser=Jsonwebtoken.verify(token.split('')[1],'s');...awaitnext()})客户端配置axios.interceptors.request.use(config=>{config.header.Authorization='token...'returnconfig;},error=>{returnPromise.reject(error);})五、配置路由删除原来的路由配置,修改为动态读取fs.readdirSync(path.join(__dirname,'./controller')).forEach(route=>{letfile=require(`./controller/${route}`)Router.use(`/${route.replace('.js','')}`,file.routes())})
