Sunday, 30 April 2017

Every reload the session resets NodeJS / A new session will be created

the whole day now I have the same error. Every time I reload the page, a new session will be created. Btw: I am a NodeJS beginner and my English is not the best.

var express = require('express');
var session = require('express-session');
var fileStore = require('session-file-store')(session);
var cookieParser = require('cookie-parser');
var app = express();

app.use(cookieParser('testing'));

app.use(session({
  name: 'Session',
  secret: 'testing',
  saveUninitialized: false,
  resave: true,
  cookie: { 
    maxAge: 60000,
    httpOnly: true,
    secure: true
  },
  store: new fileStore({
    path: './tmp/sessions/',
    useAsync: true,
    reapInterval: 5000,
    maxAge: 10000
  })
}));

app.get('/', function(req, res) {
  if(res.session) {
    req.session.t++;
    console.log(req.session.t);
  } else {
    req.session.t = 1;
    req.session.save();
    console.log(req.session.t);
  }
  res.sendFile(__dirname + 'index.html');
});

Best Regards,

Heisenberg



via Heisenberg

No comments:

Post a Comment