乐趣区

koa初探

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

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

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

router.get('/json', async (ctx, next) => {
    // http://localhost:3000/json?name=lius&age=26&sex=true
    ctx.body = {
        url: ctx.url,
        ctx_query: ctx.query,
        ctx_querystring: ctx.querystring
    }
    // {
    //     "url": "/json?name=lius&age=26&sex=true",
    //     "ctx_query": {
    //         "name": "lius",
    //         "age": "26",
    //         "sex": "true"
    //     },
    //     "ctx_querystring": "name=lius&age=26&sex=true"
    // }
})

router.post('/json',async (ctx,next)=>{await ctx.cookies.set('name',ctx.request.body.name)
    console.log('name',ctx.cookies.get('name'))
    
    ctx.body =  ctx.request.body
})
module.exports = router
退出移动版