GithubHelp home page GithubHelp logo

Losing Session Variable about nodesession HOT 3 CLOSED

quorrajs avatar quorrajs commented on August 29, 2024
Losing Session Variable

from nodesession.

Comments (3)

bloodyKnuckles avatar bloodyKnuckles commented on August 29, 2024 1

I've been testing for the presence of the session var periodically throughout the day and have not lost it yet. It looks like moving Ecstatic outside the startSession method solved the problem.

UPDATE: day two of testing, session variables remain. :)

UPDATE # 2: four days later, session variable (initiated 4 days ago) remains! Have not lost it yet.

from nodesession.

harishanchu avatar harishanchu commented on August 29, 2024

From your issue I think session is cleared by the garbage collector.

Are you sure whether cookie is valid when you loose your session?

Default session lifetime for NodeSession is 5 minutes. ie, after this time cookie will get expire. Even though session will be present in the storage until the garbage collector hits the lottery.

By default the chances of removing expired sessions from storage is twice per 100 requests(garbage collector is designed like this for performance reasons). Please see the garbage collector method of the file session driver here for more insight on this.

from nodesession.

bloodyKnuckles avatar bloodyKnuckles commented on August 29, 2024

(The problem might be solved [see below], but first...)

@harishanchu I appreciate the insight. Yes the cookie's a good place to start, but the cookie has not expired. I'm keeping close track of it on the browser, and I set NodeSession lifetime parameter to 7 days. I've been cross referencing the cookie node_session value to the session file name in the sessions directory, and the _token property value in the session file compared to the req.session.__attributes object, and all remain consistent.

However...

Digging in the code I noticed NodeSession takes over the res.end function:

https://github.com/quorrajs/NodeSession/blob/master/index.js#L82

So, I started wondering if this was an issue with Ecstatic. I'm using it to deliver static files. Last evening I tried taking Ecstatic out of the picture.

Here's my before code, in which sessionStart wraps around Ecstatic:

NodeSession.startSession(req, res, function () {
  if ( rm && 'POST' === req.method ) {
    body(req, res, function (err, pvars) {
      rmx = xtend(rmx, { params: xtend(rmx.params, pvars) })
      result = rm.fn(req, res, rmx)
    })
  }
  else if ( rm ) { result = rm.fn(req, res, rmx) }
  else { ecstatic(req, res) }
})

And my after code, where I exclude Ecstatic from the startSession callback:

if ( rm ) {
  NodeSession.startSession(req, res, function () {
    if ( 'POST' === req.method ) {
      body(req, res, function (err, pvars) {
        rmx = xtend(rmx, { params: xtend(rmx.params, pvars) })
        result = rm.fn(req, res, rmx)
      })
    }
    else { result = rm.fn(req, res, rmx) }
  })
}
else { ectstatic(req, res) }

I've been reloading, refreshing over and over yesterday and today and so far no problems. Whereas before I'd lose the session variable I set at least within, probably as you say, 100 requests, more often 10 or 20, or less.


And here's the server code (in a nutshell):

var https = require('https')
var fs = require('fs')
var path = require('path')
var xtend = require('xtend')
var url = require('url')
var body = require('body/any')
var ecstatic = require('ecstatic')(path.join(__dirname, 'public'))
var NodeSession = require('node-session')
var router = require('./lib/router.js') // using npm routes

var nodeSession = new NodeSession({
  secret: 'D?w5Sy4CJnO@Ae847l|)CgZ_W6cSIl4E',
  'lifetime': 7 * 60 * 60 * 1000,
  'secure': true,
  'encrypt': true
})

var secserver = https.createServer({
    key : fs.readFileSync('path/to/keys/agent2-key.pem'),
    cert: fs.readFileSync('path/to/keys/agent2-cert.pem')
  },
  function (req, res) {
    var rm = router.match(url.parse(req.url).pathname)
    if ( rm ) {
      NodeSession.startSession(req, res, function () {
        if ( 'POST' === req.method ) {
          body(req, res, function (err, pvars) {
            rmx = xtend(rmx, { params: xtend(rmx.params, pvars) })
            rm.fn(req, res, rmx)
            })
          }
          else { rm.fn(req, res, rmx) }
        })
      }
    else { ecstatic(req, res) }
  }
).listen(8000)

from nodesession.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.