1:路由示例:users.js
const router = require('koa-router')()
router.prefix('/users')
router.get('/', function (ctx, next) {
ctx.body = 'this is a users response!'
})
router.get('/bar', function (ctx, next) {
ctx.body = 'this is a users/bar response'
})
module.exports = router
加載
// routes
app.use(index.routes(), index.allowedMethods())
app.use(users.routes(), users.allowedMethods())
2:cookie
router.get('/', async (ctx, next) => {
global.console.log('index2')
ctx.cookies.set('pvid', Math.random())
await ctx.render('index', {
title: 'Hello Koa 2!'
})
})
router.get('/json', async (ctx, next) => {
ctx.body = {
title: 'koa2 json',
cookie: ctx.cookies.get('pvid')
}
})