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())

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-24
  • 2022-12-23
  • 2021-09-21
  • 2021-07-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案