0

I have created an Node js app, I am creating session using

app.use(session({
  resave: true , 
  rolling: true,
  secret: '2853554111' , 
  genid: function(req) { return uuid(); },
  saveUninitialized: true
 }));

and this is how I am saving my data in session.

req.session.user=req.body.username;

but after sometime I am getting undefined value of user in session, and this is hapenning very frequently, Not sure what is wrong with the code.

This is how my app.js looks like

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var bodyParser = require('body-parser');
var uuid = require("uuidv4");
var app = express();
var login = require('./routes/login-admin');
var adminHome = require('./routes/adminHome');
var pmLogin = require('./routes/pmLogin');
var pmHome = require('./routes/pmHome');
var confirmEmail = require('./routes/confirmEmail');

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static('public'));
app.use(logger('dev'));
//app.use(express.json());
app.use(express.json({limit: '5120mb'}));
//app.use(bodyParser.json())
app.use(bodyParser.json({ limit: '5120mb' }))
app.use(express.urlencoded({limit: '5120mb', extended: false }));

// view engine setup
app.use(session({
  resave: true , 
  rolling: true,
  secret: '2853554111' , 
  genid: function(req) { return uuid(); },
  saveUninitialized: true
 }));

app.use('/', indexRouter);
//app.use('/', login);
app.use('/users', usersRouter);
app.use('/login-admin', login);
app.use('/adminHome', adminHome);
app.use('/login', Login);
app.use('/Home', Home);
app.use('/confirmEmail', confirmEmail);
Doctor Who
  • 747
  • 1
  • 5
  • 28
  • _"By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting a web browser application."_ [express-session](https://www.npmjs.com/package/express-session) – jabaa May 12 '22 at 14:05
  • But I am not exiting the web browser, session is getting undefined whenever I send request to server – Doctor Who May 12 '22 at 14:08
  • After what time? – jabaa May 12 '22 at 14:10
  • Within 1 minutes – Doctor Who May 12 '22 at 14:11
  • Do you see the cookie in the browser, in the request and the response? Do you see an expiry value? – jabaa May 12 '22 at 14:13
  • @jabaa this is what I can see in server `{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}` – Doctor Who May 12 '22 at 15:15
  • And after a minute the cookie is deleted in your browser? You have to debug the problem and ask a specific question. [StackOverflow is a question-and-answer site for specific questions about actual code; “I wrote some buggy code that I can’t fix” is not a question, it’s a story, and not even an interesting story.](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) Add a [mcve] and necessary steps to reproduce the problem. – jabaa May 12 '22 at 15:23
  • @jabaa no cookie is not deleted from Browser. – Doctor Who May 12 '22 at 15:55
  • Do you see it in the request? – jabaa May 12 '22 at 16:03
  • Yes I can see it in the request, but my session in node js was undefined – Doctor Who May 12 '22 at 16:19
  • I would open `node_modules/node-session/index.js` and set a breakpoint in `NodeSession.prototype.startSession`. Then, I would start the backend with the debugger and try to find the problem from this breakpoint. Another useful breakpoint could be in `NodeSession.prototype.__getCookie`. This [block](https://github.com/quorrajs/NodeSession/blob/c5f597a7a2eaf9f7c5b56fcbb338d69b048247f5/index.js#L324) reads the cookie from the request. – jabaa May 12 '22 at 16:55

1 Answers1

1

you can use store attribute of express-session to save your session in database. multiple example is given in this link. Below is the snippet of connecting node js to Mysql

const MySQLStore = require('express-mysql-session')(session);

const options = {   
  host: "xx.xx.xx.xx",              // setting connection options
  user: 'node',
  password: "xxxxxxxx",
  database: 'xxxxx',
  socketPath: 'xxxxx' //required only if using GAE
};

const sessionStore = new MySQLStore(options);

app.use(session({
  resave: true , 
  rolling: true,
  secret: 'xxxxxx' , 
  saveUninitialized: true,
  cookie: { maxAge: 86400000  },
  store: sessionStore,
 }));

and you are good to go.

Rigsby
  • 180
  • 2
  • 14