# node-koa **Repository Path**: global_vue3_node_electron_ts/node-koa ## Basic Information - **Project Name**: node-koa - **Description**: No description available - **Primary Language**: Unknown - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-10-31 - **Last Updated**: 2023-10-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 安装koa依赖 ``` yarn add koa @types/koa ``` - koa路由安装 ``` yarn add koa-router @types/koa-router ``` - 安装ts-node ``` yarn add ts-node typescript ``` - 热重启 nodemon ``` yarn add nodemon ``` # jest单元测试 - 安装jest ``` yarn add jest @types/jest ts-jest -D ``` - vscode jest runner插件运行测试 - 网络请求测试 - package.json : "jest":{ "preset":"ts-jest" }, - tsc --init ``` yarn add supertest @types/supertest -D ``` # 配置 dotenv ``` yarn add dotenv @types/dotenv ``` - 配置根目录.env src/config下的引用 # 打印日志 - 根据官方配置、调用即可 ``` - yarn add log4js @types/log4js ``` # sequelize使用 ``` - yarn add @types/node @types/validator -D - yarn add sequelize reflect-metadata sequelize-typescript - yarn add mysql2 ``` # 参数格式校验 ``` yarn add async-validator ``` # 中间件 - middleware # 加密处理 ``` yarn add bcryptjs @types/bcryptjs ``` - 加盐加密 - 通过中间件控制 # 用户认证 - 登录成功后办法token 在以后的请求中携带 ``` yarn add jsonwebtoken @types/jsonwebtoken ``` - header 头部 - payload 荷载 - signature 签名 # redis ``` yarn add redis yarn add koa-redis koa-generic-session 操作redis依赖 操作session ``` # 接口操作流程 - 1、model -> xx.model.ts文件 - 2、service -> xx.service.ts - 3、controller -> xx.controller.ts - 4、router -> xx.router.ts - 中间件操作 -> middleware # 多表关联查询 ``` model中使用 User.belongsTo(Depart, { foreignKey: 'depart_id',as:'depart_info' }); User.belongsTo(Role, { foreignKey: 'role_id', as:'role_info'}); const res = await User.findAndCountAll({ where:whereOpt, offset: (page - 1) * size, limit: size, attributes:{ exclude:['password'],//排除密码 include:[ [Sequelize.col('depart_info.name'), 'dept_name'], [Sequelize.col('role_info.name'), 'role_name'], ] }, include:[ { model:Depart, as:'depart_info',//别名 // attributes:[['name','depart_name']], //depart:{name:''} attributes:[], //depart:{name:''} }, { model:Role, as:'role_info',//别名 // attributes:[['name','role_name']], //depart:{name:''} attributes:[], //depart:{name:''} } ] }) ``` # PM2 启动服务 - document : https://pm2.keymetrics.io/docs/usage/application-declaration/ ``` - 全局安装 pm2 npm install -g pm2 - pm2 安装typescript和ts-node pm2 install typescript pm2 install ts-node@latest - typescript 安装失败 就先装ts-node@latest,在安装typescript ```