Apply same express session on multiple node js app

Viewed 421

i'm working on micro services with express js with express-session set up like this:

server.use(session({
    name: 'apps',
    resave: false,
    saveUninitialized: false,
    secret: 'secretToken',
    cookie: {
        sameSite: false,
        secure: false, // true
        httpOnly: false, // false
    }
}));

i apply the same setup with all services. but then it's only work with just only one services. other services can't share the same data from other service, i'm putting access token in the req.session to let other apps having the same access token.

Is there a way for me to let different service using the same session?

1 Answers

The default server-side session storage is MemoryStore. Which means each service store the session data on their own process. Memory cannot be shared between different processes.

In order for microservices to share session data, we need separate storage, such as connect-redis, connect-mongo.

All microservices connect the redis or database server and use them as session data storage when initializing express-session middleware.

Workflow:

Client send HTTP request(with session id stored in a cookie) => Service A or Service B or Service C => express-session get the session data from redis or database by session-id => do your things.

Related