Giter Site home page Giter Site logo

hapi-redis2's Introduction

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://:[email protected]: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://[email protected]: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

hapi-redis2's People

Contributors

dependabot[bot] avatar mautematico avatar mehdikarimian avatar midnightcodr avatar ricochenft avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.