Fastify Redis connection plugin, with this you can share the same Redis connection in every part of your server.
Under the hood the official redis client is used, the options
that you pass to register
will be passed to the Redis client.
npm i fastify-redis --save
Add it to you project with register
and you are done!
You can access the Redis client via fastify.redis
.
If needed, you can pass a custom driver
option, such as ioredis. By default the official redis client is used.
const fastify = require('fastify')
fastify.register(require('fastify-redis'), {
driver: require('ioredis'),
host: '127.0.0.1'
}, err => {
if (err) throw err
})
fastify.get('/foo', (req, reply) => {
const { redis } = fastify
redis.get(req.query.key, (err, val) => {
reply.send(err || val)
})
})
fastify.post('/foo', (req, reply) => {
const { redis } = fastify
redis.set(req.body.key, req.body.value, (err) => {
reply.send(err || { status: 'ok' })
})
})
fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
This project is kindly sponsored by:
Licensed under MIT.