3.1.2 • Published 8 months ago

hapi-redis2 v3.1.2

Weekly downloads
157
License
ISC
Repository
github
Last release
8 months ago

About

Inspired by https://github.com/Marsup/hapi-mongodb, here's an ioredis plugin for hapijs that supports multiple connections.

Notes

  • Starting from version 2, this plugin switches to ioredis as the redis client
  • Starting from version 1.0.0 this plugin only supports Hapi version 17 and above. If you are using hapijs prior to version 17, please check out version 0.9.11-a

Options

  • decorate: string or boolean, mixed use of different types of decorate settings are not allowed.
  • settings: can be

A) null, redis client will be initialized with new Redis(), which connects to the redis server running on localhost at port 6379 without authentication.

B) an object,

{
  port: 6379,          // Redis port
  host: '127.0.0.1',   // Redis host
  family: 4,           // 4 (IPv4) or 6 (IPv6)
  password: 'auth',
  db: 0
}

C) a url string,

'redis://:auth@127.0.0.1:6379/4'

Note prior to v2, the url option was used.

Installation

npm install hapi-redis2

Example

const Hapi = require('@hapi/hapi')
const Boom = require('@hapi/boom')

const launchServer = async function() {
    const clientOpts = {
        settings: 'redis://secret@127.0.0.1:6379/2',
        decorate: true
    }

    const server = Hapi.Server({ port: 8080 })

    await server.register({
        plugin: require('hapi-redis2'),
        options: clientOpts
    })

    server.route({
        method: 'GET',
        path: '/redis/{val}',
        async handler(request) {
            const client = request.redis.client

            try {
                await client.set('hello', request.params.val)
                return {
                    result: 'ok'
                }
            } catch (err) {
                throw Boom.internal('Internal Redis error')
            }
        }
    })

    await server.start()
    console.log(`Server started at ${server.info.uri}`)
}

launchServer().catch(err => {
    console.error(err)
    process.exit(1)
})
   

Check out lib/index.test.js for more usage examples.

Requirements:

@hapi/hapi>=20

nodejs>=8
3.1.2

8 months ago

3.1.0

1 year ago

3.0.1

3 years ago

3.0.0

3 years ago

2.2.3

3 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.7

5 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.9.11

7 years ago

0.9.10

7 years ago

0.9.9

7 years ago

0.9.8

7 years ago

0.9.7

7 years ago

0.9.6

7 years ago

0.9.5

7 years ago

0.9.4

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago