KOA
1.KOA项目搭建
脚手架搭建
cnpm install -g koa-generator
koa2 +项目名称
cd 项目名称 && npm install
cnpm install --update-binary // 意义
运行命令
npm run dev
安装mysql2链接
cnpm install sequelize mysql2 –save
安装跨域包
cnpm install koa-cors –save
一个简单的KOA项目搭建成功
2.KOA项目使用
Sequelize数据库链接使用
中自带的API方法例:
create,findOne,delete,patch,等
Koa Context
ctx 这是 Context
ctx.requese 这是 koa Request (path,method)
ctx.response 这是 koa Response (length,type)
ctx.state 好像存储状态树,例如ctx.state.user= {} 赋值后可以在别的service中使用
ctx.throw 错误抛出
Get和post请求参数接收方式
post请求
const data = ctx.request.body;
const username = data.username
const password = data.password
get请求
const query = ctx.request.query;
const username = query.username
const password = query.password
EGGJS
1. egg项目搭建
cd egg-example
cnpm i egg-init -g
egg-init 项目名称--type=simple
cd 项目名称
cnpm i
npm run dev
安装mysql链接
cnpm i --save egg-mysql
2. egg项目使用
数据库连接配置
JWT链接配置
Ignore:白名单
Middleware:中间件
项目结构
3. 登录接口测试
输入密码错误时
输入密码正确时
测试通过