Nodejs load balance through nginx using redis as session storage

I tried loading balance in Nodejs via nginx using redis as a session repository.

I use 3 Machine

  • Machine A (192.168.14.65) - Nginx, Redis installs
  • Installed mechanism B (192.168.14.92) - nodejs
  • C machine installed (192.168.9.9) - nodejs

I set the session variable in Machine B, and I'm trying to access the same session variable through Machine C.

I use redis as a session repository, so I have to access the variable in Machine B.

Encoding machine B (file name as app.js)

var express = require('express');
var redis   = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');
var client  = redis.createClient();
var app = express();

app.use(session({
    secret: 'ssshhhhh',
    // create new redis store.
    store: new redisStore({ host: '192.168.14.65', port: 6379, client: client,ttl :  260}),
    saveUninitialized: false,
    resave: false
}));


app.get('/', function (req, res) {
  console.log(req.session);
  req.session.helloworld = 'helloworld';
  res.send('set session');

});

app.listen(3000,function(){
    console.log("App Started on PORT 3000");
});

encoding for Machine C (file name as app.js)

var express = require('express');
var redis   = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');
var client  = redis.createClient();
var app = express();

app.use(session({
    secret: 'ssshhhhh',
    // create new redis store.
    store: new redisStore({ host: '192.168.14.65', port: 6379, client: client,ttl :  260}),
    saveUninitialized: false,
    resave: false
}));


app.get('/', function (req, res) {
  if(req.session.helloworld) {
    res.send('get session: '+ req.session.helloworld);
  } else {
    res.send('no session: ');
  } 

});

app.listen(3000,function(){
    console.log("App Started on PORT 3000");
});

and I have package.json in both machines

package.json code

{
  "name": "Node-Session-Redis",
  "version": "0.0.1",
  "scripts": {
    "start": "node ./bin"
  },
  "dependencies": {
    "async": "^1.2.1",
    "body-parser": "^1.13.0",
    "connect-redis": "^2.3.0",
    "cookie-parser": "^1.3.5",
    "ejs": "^2.3.1",
    "express": "^4.12.4",
    "express-session": "^1.11.3",
    "mysql": "^2.7.0",
    "redis": "^0.12.1"
  }
}

I run the code using the command

node app.js

Everything works fine in car B.

Machine C.

TypeError: Cannot read property 'helloworld' of undefined
    at /home/local/PAYODA/balachandran.v/js/app.js:19:17
    at Layer.handle [as handle_request] (/home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/layer.js:95:5)
    at /home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/index.js:330:12)
    at next (/home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/index.js:271:10)
    at session (/home/local/PAYODA/balachandran.v/js/node_modules/express-session/index.js:159:61)
    at Layer.handle [as handle_request] (/home/local/PAYODA/balachandran.v/js/node_modules/express/lib/router/layer.js:95:5)

req.session , B .

{ cookie:     { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true } }

C

undefine

- , ?

+4

All Articles