关于Koa

240 阅读1分钟

优点

koa优化点在于ctx上下文和洋葱模型 封装了req res等api。

安装

npm install koa -S

hello word

async和await

1.async函数直接return返回Promise

2.await不仅仅接受promise还可以接受普通值

3.async是声明一个方法是异步 await是等待异步方法完成

get请求参数获取

post请求参数获取(原生)

post请求参数获取(中间件koa-bodyparser)

1.下载包: npm install koa-bodyparser -S

原生路由

路由中间件koa-router

  1. 下载包: npm install koa-router -S
  1. 前缀(一般都是全局的)
  1. 子路由

cookie

  • domain:写入cookie所在的域名
  • path:写入cookie所在的路径
  • maxAge:Cookie最大有效时长
  • expires:cookie失效时间
  • httpOnly:是否只用http请求中获得
  • overwirte:是否允许重写

ejs

下载包: npm install koa-views -S npm install ejs -S

静态资源

下载包: npm install koa-static -S