• get
  • post get post数据解析
    bodyparser配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    const bodyparser = require('koa-bodyparser')

    app.use(bodyparser({
    enableTypes:['json', 'form', 'text']
    }))

    //get
    router.get('/get',async(ctx, next)=>{
    let request = ctx.request
    var datas = JSON.stringify(request.query) !== '{}' ? request.query : {}
    ctx.body = Object.assign(datas,{
    host : request.header.host,
    url : request.url,
    })
    })
    //post
    router.post('/post',async(ctx, next)=>{
    let request = ctx.request
    console.log(request.body)
    var datas = JSON.stringify(request.body) !== '{}' ? request.body : {}
    ctx.body = Object.assign(datas,{
    host : request.header.host,
    url : request.url,
    })
    })