Koa.js で加算

こちらのプログラムと同じことを Koa.js で行いました。
[Nuxt.js で簡単な WebAPI を作成 (その 2)](https://qiita.com/ekzemplaro/items/09191b15fdc0978c4e20)

フォルダー構成

```text
$ tree
.
├── app.js
└── sum_number.js
```

```js:app.js
// ---------------------------------------------------------------
//	app.js
//
//					Dec/28/2020
// ---------------------------------------------------------------
const Koa = require('koa')
const koaBody = require('koa-body')
const app = new Koa()

console.error ("*** sum_number *** start ***")

// middleware functions
app.use(koaBody())


// Require the Router we defined in sum_number.js
let sum_number = require('./sum_number.js')

// Use the Router on the sub route /sum_number
app.use(sum_number.routes())

// Bootstrap the server
app.listen(3000)
// ---------------------------------------------------------------
```

```js:sum_number.js
// ---------------------------------------------------------------
//	sum_number.js
//
//					Dec/28/2020
// ---------------------------------------------------------------
const Router = require('koa-router')

// Prefix all routes with /sum_number
const router = new Router({
	prefix: '/sum_number'
})

// ---------------------------------------------------------------
// Routes will go here
router.get('/', (ctx, next) => {
	ctx.body = {
		status: 'success',
		message: 'root'
	}
	next()
})

// ---------------------------------------------------------------
router.get('/sum', (ctx, next) => {
	console.error ("*** get *** /sum ***")
	const aa = parseInt(ctx.query.aa,10)
	const bb = parseInt(ctx.query.bb,10)
	const sum = aa + bb

	console.error ("ctx.query.aa = " + ctx.query.aa)
	console.error ("ctx.query.bb = " + ctx.query.bb)
	console.error ("sum = " + sum)

	ctx.body = {
			status: 'success',
			aa: aa,
			bb: bb,
			sum: sum
			}
	next()
})

// ---------------------------------------------------------------
router.post('/sum', (ctx, next) => {
	console.error ("*** post *** /sum ***")
	// Check if any of the data field not empty
	if (
		!ctx.request.body.aa ||
		!ctx.request.body.bb
	) {
		ctx.response.status = 400
		ctx.body = {
			status: 'error',
			message: 'Please enter the data'
    }
	} else {
			const aa = parseInt(ctx.request.body.aa,10)
			const bb = parseInt(ctx.request.body.bb,10)
			const sum = aa + bb
		ctx.response.status = 201
		ctx.body = {
			status: 'success',
			aa: aa,
			bb: bb,
			sum: sum
		}
	}
	next()
})

module.exports = router

// ---------------------------------------------------------------
```

サーバーの起動

```bash
export NODE_PATH=/usr/lib/node_modules
node app.js
```

テスト結果

GET

```text
$ curl -X GET 'http://localhost:3000/sum_number/sum?aa=21&bb=15' | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    45  100    45    0     0  22500      0 --:--:-- --:--:-- --:--:-- 22500
{
  "status": "success",
  "aa": 21,
  "bb": 15,
  "sum": 36
}
```

POST

```text
$ curl -X POST http://localhost:3000/sum_number/sum -d 'aa=23' -d 'bb=49'  | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    45  100    11  15000   3666 --:--:-- --:--:-- --:--:-- 18666
{
  "status": "success",
  "aa": 23,
  "bb": 49,
  "sum": 72
}
```