forked from amjanoni/rest-key-value
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (46 loc) · 1.09 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env node
import { createClient } from 'redis'
import express from 'express'
// config map
const config = {
PORT: process.env.PORT || 3000,
REDIS_HOST: process.env.REDIS_HOST || 'localhost',
REDIS_PORT: process.env.REDIS_PORT || 6379,
}
async function main({ config }) {
// express server
const app = express()
// redis client
const redis = createClient({
socket: {
host: config.REDIS_HOST,
port: config.REDIS_PORT,
}
})
await redis.connect()
// routes
app.get('/', (req, res) => {
res.json({
status: 'OK',
message: `Hello from ${process.env.npm_package_name}`
})
})
app.get('/data/:key', async (req, res) => {
const reply = await redis.get(req.params.key)
res.json({
status: 'OK',
data: reply
})
})
app.put('/data/:key/:value', async (req, res) => {
await redis.set(req.params.key, req.params.value)
res.json({
status: 'OK'
})
})
// start server
app.listen(config.PORT, () => {
console.log(`Server is listening at http://localhost:${config.PORT}`)
})
}
main({config})