Redis の WebAPI (Express)

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
[Redis の WebAPI を作成](https://qiita.com/ekzemplaro/items/1746b31beef382023f2d)

## サーバープログラム

フォルダー構成

```text
$ tree
.
├── app.js
└── routes
    └── index.js
```

```js:app.js
//-------------------------------------------------------------------------
//	app.js
//
//					May/30/2023
//-------------------------------------------------------------------------
var express = require('express')
var routes = require('./routes')
var cfenv = require('cfenv')

var app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.use(express.static(__dirname + '/public'))

const appEnv = cfenv.getAppEnv()

app.post('/read',routes.read)
app.post('/list',routes.list)
app.post('/insert',routes.insert)

app.listen(appEnv.port, '0.0.0.0', function() {
  console.log("server starting on " + appEnv.url)
})

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

```js:routes/index.js
// -----------------------------------------------------------------------
/*
	routes/index.js

						May/29/2023
*/
// -----------------------------------------------------------------------
const redis = require("redis")
const client = redis.createClient()

exports.read = async function(req,res)
{
	console.error ("*** read *** start ***")
	await client.connect()

	if (req.body.key) {
		key = req.body.key
		}

	var dict_aa = {}

	const value = await client.get(key)
	dict_aa["key"] = value

	const str_out = JSON.stringify(dict_aa)

	res.send(str_out)

	console.error ("*** read *** end ***")

	await client.disconnect()
}

// -----------------------------------------------------------------------
exports.list = async function(req,res)
{
	console.error ("*** list *** start ***")
	await client.connect()

	const keys = await client.keys('*')

	const str_out = JSON.stringify(keys)
	res.send(str_out)
	console.error ("*** list *** end ***")

	await client.disconnect()
}

// -----------------------------------------------------------------------
exports.insert = async function(req,res)
{
	console.error ("*** insert *** start ***")
	await client.connect()

	var key = ""
	var value = ""

	if (req.body.key) {
		key = req.body.key
		}

	if (req.body.value) {
		value = req.body.value
		}

	await client.set(key, value, redis.print)

	res.send(value)
	await client.disconnect()
	console.error ("*** insert *** end ***")
}

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

## サーバーの起動

```text
$ export NODE_PATH=/usr/local/lib/node_modules
$ node app.js 
server starting on http://localhost:3000
```

## テストスクリプト

key の一覧

```bash:go_list.sh
#
URL=http://localhost:3000/list
#
curl -X POST $URL
#
echo
#
http POST $URL
#
echo
```

書き込み

```bash:go_insert.sh
#
URL=http://localhost:3000/insert
#
curl -X POST -d key="t1855" \
	-d value="{"name": "宇都宮", "population": 27695, "date_mod": "2003-2-2"}" \
	$URL
#
echo
#
http $URL key="t1856" \
	value="{"name": "小山", "population": 65491, "date_mod": "2003-3-21"}" 
#
#
```

読み出し

```bash:go_read.sh
#
URL=http://localhost:3000/read
#
curl -X POST -d key="t1855" $URL
#
echo
#
curl -X POST -H "Content-Type: application/json" \
	-d '{"key":"t1856"}'  $URL
#
echo
#
curl -X POST -H "Content-Type: application/json" \
	-d@in01.json $URL
#
echo
#
http $URL key="t1858"
```

```json:in01.json
{"key":"t1857"}
```

## 確認したバージョン

```text
$ node --version
v20.2.0
```