Ioredis for Fastify.
npm i fastify-ioredisjs --save
Add it to you project with register
and you are done!
You can access the Redis client via fastify.redis
.
More usage see Ioredis
const fastify = require('fastify')
fastify.register(require('fastify-ioredisjs'), {
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}`)
})
Licensed under MIT.