0
0

More than 1 year has passed since last update.

Redis の WebAPI (Express)

Last updated at Posted at 2020-01-21

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
Redis の WebAPI を作成

サーバープログラム

フォルダー構成

$ tree
.
├── app.js
└── routes
    └── index.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)
})

//-------------------------------------------------------------------------
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 ***")
}

// -----------------------------------------------------------------------

サーバーの起動

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

テストスクリプト

key の一覧

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

書き込み

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"}" 
#
#

読み出し

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"
in01.json
{"key":"t1857"}

確認したバージョン

$ node --version
v20.2.0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0