-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
34 lines (30 loc) · 1.11 KB
/
example.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
const Fastify = require('fastify')
const fastifyRedis = require('fastify-redis')
const plugin = require('./index')
const nodeRedis = require('redis')
const { join } = require('path')
const { createClient } = nodeRedis
const build = (options) => {
const instance = Fastify()
const client = createClient({ host: 'redis-test' })
instance
.register(fastifyRedis, { client })
.register(plugin, { path: join(__dirname, 'test-scripts') })
.get('/hello/:name', {}, (req, reply) => {
const { redis, scripts } = instance
redis.evalsha(scripts.hello.sha, 0, req.params.name, (err, result) => {
reply
// hello.lua script returns JSON which do not need seraialization.
// Therefore we can bypass fastify internal serialization process.
// For that we must set the content-type header.
// See: https://www.fastify.io/docs/latest/Reply/#-serializer-func-
.type('application/json; charset=utf-8')
.serializer(function () {
return result
})
.send(err || result)
})
})
return instance
}
module.exports = build