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
    原文作者:liuoomei
    原文地址: https://segmentfault.com/a/1190000019640621
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞