koa路由接口

const router = require('koa-router')()
//返回一个页面
router.get('/', async (ctx, next) => {
  global.console.log('index11111');
  await ctx.render('index', {    //render返回一个页面
    title: 'Hello Koa 2!'
  })
})
//直接返回数据的用body,json数据
router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})
//在不同页面为了区分接口,我们可以在加个前缀/user
router.prefix('/users')
module.exports = router
app.js
app.use(index.routes(), index.allowedMethods())
原文地址:https://www.cnblogs.com/lanshu123/p/10703868.html