Repository: expressjs/session

Issue #474: Express session with express-mysql-session not working at all
Created: 2017-06-25T11:40:20Z
I learnt PHP sessions within minutes but can't use this express session even after trying for two days. I'm really stuck and it's wasting my time :(
The problem is that it saves session only first time with any request. On page reload from browser, the session ID changes, hence the old value that I stored in the session becomes undefined. Even in DB the session id keeps changing (i.e., new entries on every page reload). I've tried many many solutions gives on github and here but none of them worked for me. Tried resave rolling maxAge etc. but none worked.
Here's my code:
var express = require('express');
var mysql = require('mysql');
var session = require('express-session');
var MySQLStore = require('express-mysql-session')(session);
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json({type: '*/*'}));
var http = require('http');


app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    next();
});

var connectionPool = mysql.createPool({
        host: '127.0.0.1',
        user: 'root',
        password: '123456',
        database: 'cs_chat',
        connectionLimit: 2,
    });

var options = {
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'cs_chat',
    schema: {
        tableName: 'my_session',
        columnNames: {
            session_id: 'session_id',
            expires: 'expires_column_name',
            data: 'data_column_name'
        }
    }
};
 
var sessionStore = new MySQLStore(options);

var server=http.createServer(app);

app.use(session({
  secret: 'some secret',
  resave: false,
  saveUninitialized: true,
  store: sessionStore,
  cookie: {
    secure: false,
    maxAge: 36000000,
    httpOnly: false,
  },
}));


app.get("/test", function(req, res, next){
     console.log(req.session);
  console.log(req.sessionID, req.session.name);
    if(!req.session.name){
        console.log("Not set.");
        req.session.name = "vikas kumar";
    } else {
        console.log("Already set");
        console.log(req.session.name);
    }
    res.end();
});

server.listen(3000, '0.0.0.0', function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log("testApp socket server is listening at http://%s:%s", host, port+"\n\n");
});

In the console, I see Not set every time I make request to http:/192.168.43.200:3000/test using Jquery Ajax.

Duplicate issue #475: Why does express session gets destroyed after every Ajax request?
Created: 2017-06-25T11:45:02Z
I'm trying to use express-session but it is not working the way I want. For example, if I visit http://127.0.0.1:8081/test from browser 3 times, I see following response in browser:
Not set
Already set
Already set
The means seesionID for is unique and session is not destroyed when I visit this page again and again.
However, the scenario changes when I request the same page using Ajax, which I have wrote in test.php file:
test.php
<script type="text/javascript">
  $(document).ready(function(){
    setInterval(function(){
    $.ajax({
      type: 'get',
      url: 'http://127.0.0.1:8081/test',
      success: function(data){
        console.log(data);
      },
      error: function(aa, bb, cc){
                  // console.log("Ajax error", aa, bb, cc);
      	}
      });
     }, 5000);
  });
</script>

Server side code:
var express = require('express');
var mysql = require('mysql');
var session = require('express-session');
// var MySQLStore = require('express-mysql-session')(session);
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json({type: '*/*'}));
var http = require('http');
app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    next();
});

var server=http.createServer(app);

app.use(session({
  secret: 'some secret',
  resave: false,
  // rolling: true,
  saveUninitialized: true,
  cookie: {
    secure: false,
    maxAge: 36000000,
    httpOnly: false,
  },
}));


app.get("/test", function(req, res, next){
     console.log(req.session);
  console.log(req.sessionID, req.session.name);
    if(!req.session.name){
        console.log("Not set.");
        res.write("Not set");
        req.session.name = "vikas kumar";
    } else {
        console.log("Already set");
        res.write("Already set");
        console.log(req.session.name);
    }
    res.end();
});

server.listen(8081, function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log("testApp socket server is listening at http://%s:%s", host, port+"\n\n");
});

From this, I see following response in browser's console:
Not set
Not set
Not set ... And so on.
Note: I'm not using default Memory Store for session.
Am I doing something wrong? Or it has something to do with Ajax request?

Closing comment (2017-07-04T04:19:16Z): @vikaskumar2299 said this is a duplicate of #474