0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redis の WebAPI (Express,TypeScript)

Last updated at Posted at 2020-10-15

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
Redis の WebAPI を作成
次のプログラムを TypeScript に変更しました。
Redis の WebAPI (Express)

サーバープログラム

フォルダー構成

$ tree
.
├── app.ts
└── routes
    └── index.ts
app.ts
//-------------------------------------------------------------------------
//	app.ts
//
//					May/30/2021
//-------------------------------------------------------------------------
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.ts
// -----------------------------------------------------------------------
/*
	routes/index.ts

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

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

	var key:string = "" 

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

	var dict_aa: {[key: string]: any} = {}

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

	const str_out = JSON.stringify(dict_aa)

	res.send(str_out)

	await client.disconnect()
	console.error ("*** read *** end ***")
}

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

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

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

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

	var key:string = ""
	var value:string = ""

	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
$ ts-node app.ts
server starting on http://localhost:3000

確認したバージョン

$ node -v
v20.2.0

$ tsc -v
Version 4.8.4
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?