koa2路由

注意:必须导出

文档地址:https://npm.taobao.org/package/koa-router

 

 

例:

const router = require('koa-router')()


router.prefix('/users')     //设置前缀

router.get('/', async (ctx, next) => {
  // global.console.log('index');
  
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = {
    title:'koa2 string'
  }
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})


module.exports = router

 

posted @ 2019-11-27 15:40  LIULIULIU666  阅读(195)  评论(0编辑  收藏  举报