Redis的WebAPI(Express)
这是一个满足我们规定的规范的服务器端程序,用于创建 Redis 的 WebAPI。
服务器程序
文件夹架构
$ tree
.
├── app.js
└── routes
└── index.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
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
考虑到您的要求,以下是该短语的一个可能的汉语表达方式:测试脚本 。
关键字列表
#
URL=http://localhost:3000/list
#
curl -X POST $URL
#
echo
#
http POST $URL
#
echo
写入
#
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"}"
#
#
阅读
#
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"
{"key":"t1857"}
确认的版本
$ node --version
v20.2.0