koa2 基础知识入门(一)

93 阅读1分钟

全局安装koa脚手架

npm install -g koa-generator

创建项目(模板引擎ejs)

koa2 -e 项目名

参数解析

动态路由参数

/users/3

router.post('/string/:id', async (ctx, next) => {
  const id = ctx.params.id;
  ctx.body = courseData.filter(course => course.id == id)
})

ctx.params

键值对

/users?start=1&end=20 ctx.query

router.get('/key', async (ctx, next) => {
  const { start = 0, end = 100 } = ctx.query
})

body参数

router.post('/ccc', async (ctx, next) => {
  const query = ctx.request.body
})

form-data

const files=ctx.request.files;