Nodejs

Does hapi js uses redis to share session?

빠빠담 2020. 7. 16. 11:35
반응형

https://stackoverflow.com/questions/34454899/does-hapi-js-uses-redis-to-share-session

 

Does hapi js uses redis to share session?

Redis is commonly used by expressJS to share session across multiple node js processes. Does hapiJs use similar technique?

stackoverflow.com

Redis is commonly used by expressJS to share session across multiple node js processes. Does hapiJs use similar technique?

 

==========================================================================

 

Before I answer your question, I want to give a little background. Yes, you can use Redis for sharing sessions but first be sure it's what you really want/need Things are a bit different with hapi and Express when it comes to sessions.

If you've already decided this is the setup you want, skip to part 2.

1. Background

In Express, the only thing stored in your cookie is your session ID. This means when you need to scale beyond a single server, you need to look at sharing sessions between them somehow. Redis is one option.

Note Session data is not saved in the cookie itself, just the session ID. Session data is stored server-side.

https://github.com/expressjs/session#sessionoptions

The official session plugin for hapi Yar, takes a slightly different approach than Express. As long as the data that you're trying to store in a session is below maxCookieSize, the entire session will be encrypted (via Iron) and stored in a cookie. This means you don't need any extra machinery server-side to scale horizontally because the client remembers everything.

However, if this doesn't fit your requirements or you just don't like it, you can force server-side storage by setting Yar's maxCookieSize to 0.

maxCookieSize - maximum cookie size before using server-side storage. Defaults to 1K. Set to zero to always use server-side storage.

https://github.com/hapijs/yar/blob/master/API.md

2. Using Redis with Yar and Server-side storage

When using Yar with server-side storage, hapi will use its cache functionality to store your data. To use Redis for example, just pass along a Redis cache configuration to Yar:

 

const Hapi = require('hapi');
const Hoek = require('hoek');

const server = new Hapi.Server({
    cache: {
        engine: require('catbox-redis'),
        name: 'session',                  // cache name
        host: '127.0.0.1',                // redis host
        port: 6379,                       // redis port
        database: 'my-db'                 // redis-db
    }
});

server.register({
    register: require('yar'),
    options: {
        maxCookieSize: 0,                 // force server-side storage
        cache: {
            cache: 'session'
        },
        cookieOptions: {
            password: 'U32KuiKPnVguRKM',  // cookie password
            isSecure: false               // allow non HTTPS
        }
    }
}, (err) => {

    Hoek.assert(!err, err);
    server.start((err) => {

        Hoek.assert(!err, err);
        console.log('Started server');
    });
});
반응형