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())
相关文章
相关标签/搜索