How to use koa-body

Comprehensive koa-body code examples:

How to use koa-body.ck:

60
61
62
63
64
65
66
67
68
69
70
71


router.post('/api/update', body(), async (ctx) => {
    const body = ctx.request.body;
    const eid = body.eid;
    const username = body.username;
    const ck = body.ck;
    ctx.body = await new User({eid, ck, username}).update();
});


router.post('/api/disable', body(), async (ctx) => {

How to use koa-body.username:

59
60
61
62
63
64
65
66
67
68
69
});


router.post('/api/update', body(), async (ctx) => {
    const body = ctx.request.body;
    const eid = body.eid;
    const username = body.username;
    const ck = body.ck;
    ctx.body = await new User({eid, ck, username}).update();
});

How to use koa-body.token:

79
80
81
82
83
84
85
86
87
88
89
90
    ctx.body = await new User({eid}).enableEnv();
});


router.post('/api/verifyToken', body(), async (ctx) => {
    const body = ctx.request.body;
    const token = body.token;
    ctx.body = await new User({token}).verifyToken();
});



How to use koa-body.koaBody:

77
78
79
80
81
82
83
84
85
86
const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const { koaBody } = require('koa-body');

router.post('/users', koaBody(), (ctx) => {
  console.log(ctx.request.body);
  // => POST body
  ctx.body = JSON.stringify(ctx.request.body);
});

How to use koa-body.wseid:

133
134
135
136
137
138
139
140
141
142
143
  ctx.body = { data };
});


router.post("/api/WSCKDelaccount", body(), async (ctx) => {
  const body = ctx.request.body;
  const wseid = body.wseid;
  const user = new User({ wseid });
  const data = await user.delWSCKUserByEid();
  ctx.body = { data };
});

How to use koa-body.remark:

96
97
98
99
100
101
102
103
104
105
106
});


router.post("/api/update/remark", body(), async (ctx) => {
  const body = ctx.request.body;
  const eid = body.eid;
  const remark = body.remark;
  const user = new User({ eid, remark });
  const data = await user.updateRemark();
  ctx.body = { data };
});

How to use koa-body.eid:

58
59
60
61
62
63
64
65
66
67
68
    ctx.body = await new User({eid: ctx.request.body.eid}).delUserByEid();
});


router.post('/api/update', body(), async (ctx) => {
    const body = ctx.request.body;
    const eid = body.eid;
    const username = body.username;
    const ck = body.ck;
    ctx.body = await new User({eid, ck, username}).update();
});