Skip to content

koa learning from zero and official website of Koa

Notifications You must be signed in to change notification settings

zhelingwang/myKoa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

myKoa

koa learning from zero and official website of Koa (koa + mongoDB/mongoose + redis/koa-redis)

1.初始化项目

  • 配置npm命令(package.json)
"scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
}

2.中间件 - app.use(middleware function)

  • async-await function
  • common function

3.app.keys

设置签名cookie密钥

4.app.context

//app.context : ctx 的原型,常用于挂载全局使用的属性或方法
{ 
    request:{ 
        method: 'GET',
        url: '/',
        header:{ 
            host: 'localhost:4000',
            connection: 'keep-alive',
            pragma: 'no-cache',
            'cache-control': 'no-cache',
            'upgrade-insecure-requests': '1',
            'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            accept:'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
            cookie:'guan=this is a cookie from server; guan.sig=wl4ipAgL-o4O1w2Rac4dRsGXZj0ra02baMsgzTL04iQ' 
        }
    },
  response: { status: 404, message: 'Not Found', header: {} },
  app: { subdomainOffset: 2, proxy: false, env: 'development' },
  originalUrl: '/',
  req: '<original node req>',
  res: '<original node res>',
  socket: '<original node socket>' 
}

ctx.request: koa的request对象
ctx.response: koa的response对象

ctx.req: node的request对象
ctx.res: node的response对象

Koa 不支持 直接调用底层 res 进行响应处理。请避免使用以下 node 属性:
ctx.res.statusCode
ctx.res.writeHead()
ctx.res.write()
ctx.res.end()

5.cookies

ctx.cookies.get(name, [options])

ctx.cookies.set(name, value, [options])

6.抛出错误

ctx.throw(400);
ctx.throw(400, 'name required');
ctx.throw(400, 'name required', { user: user });

ctx.assert(ctx.state.user, 401, 'User not found. Please login!');

7.自定义中间件

app.use(function(ctx,next) {
  //......
});
app.use(async(ctx,next)=>{
	//......
})
// 只需要返回一个函数作为app.use()的参数即可,所谓的中间件不过是一个函数,
// 该中间件函数用于某些特定的功能,比如:logger,解析request数据,挂载额外扩展的信息等等

8.redis and session

启动redis : redis-server

获取 ioRedis 客户端对象
const Store = new Redis().client;

const st = await Store.hset('fix','name',Math.random());
saved! 使用redis可视化工具查看即可

Store.hget("fix","name").then(res=>{
    console.log(res);
});

详细操作可查看 package : ioredis

9.static resource

app.use(require('koa-static')(__dirname + '/public'));

10.模板引擎

// 模板引擎 Must be used before any router is used
app.use(require('koa-view')(__dirname + '/views'));

11.router路由

var app = new Koa();
var router = new Router();
router.get('/', (ctx, next) => {
  // ctx.router available
});
app
  .use(router.routes())
  .use(router.allowedMethods());
//为了方便维护管理,一般将router拿出来单独成一个模块
//将router实例导出即可

12.mongoose

//api : https://mongoosejs.com/docs/models.html

//1.定义Schema来描述model即Collection的数据结构
const UserSchema = new mongoose.Schema({
	name : String,
	phone : Number,
	age : Number
});
//1.定义的属性在 mongoDB 中不一定都存在,
    //1.若某些属性在 create 时没有为其指定值,那么Collection中不存在该属性,
    //2.若某些属性在 create 时赋值为 ""/null , 那么Collection中存在该属性且值为 null
    //3.除定义的属性外其他属性不会被mongoose保存
    //4.在初始化时应注意属性的数据类型,如:Number赋值为空串,Collection中其值为0
//视具体情况而定,多留心数据类型

//2.创建model
const User = mongoose.model('UserModel', UserSchema)
//UserModel : CollectionName in DB
//返回的User是一个Class,其包含对该Collection进行各种操作的api

13.koa-generator

本项目是从零搭建一个基于Koa的项目结构,为的是更好的理解Koa的项目构建过程,实际开发中直接
使用专门的生成器模板来高效的搭建项目.

About

koa learning from zero and official website of Koa

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published