How to get SessionID in NodeJS with multiple servers?

I am new to NodeJS. I am developing a REST API and using express session to work with sessions. So, to get the session id that I am using

var sessionID = req.sessionID

This sessionID is generated from the server side. Therefore, when I scale to two or more servers, this is a problem. For example, if one server shuts down and the request is redirected to another server (suppose I have a load balancer), a new session identifier is generated. So, is there a way to get the session identifier from the client side?

+4
source share
1 answer

! , - , , node, , . , MongoDB:

'use strict';

var express = require('express'),
  session = require('express-session'),
  cookieParser = require('cookie-parser'),
  mongoStore = require('connect-mongo')(session),
  mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/someDB');

var app = express();

var secret = 'shhh';

app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: secret,
  store: new mongoStore({
    mongooseConnection: mongoose.connection,
    collection: 'sessions' // default
  })
}));

// ROUTES, ETC.

var port = 3000;

app.listen(port, function() {
  console.log('listening on port ' + port + '.')
});

req.sessionID, , cookie .

, !

+5

Source: https://habr.com/ru/post/1609732/


All Articles