Nodejs会话未定义

时间:2016-04-11 00:59:03

标签: javascript node.js session

我可以将会话变量设置为直接输出,但是如果我再次访问路径(甚至没有刷新页面),var将再次变为未定义。我错过了什么?即使刷新页面,我怎样才能保留var?

在app.js

var session = require('express-session');
app.use(session({secret: 'asecret',
  cookie: {
    path: '/',
    expires: false, // Alive Until Browser Exits
    httpOnly: true
  }}));

在Index.js

var sess ="";

router.get('/getSubmit', function(req, res){
    var socket = req.app.get('socketio');
    sess = req.session;

   sess.avar = "stuff";
   console.log(sess.avar); //prints 

  console.log(sess.test); //will not ever print although has been set
  if (sess.test == undefined) {
      sess.test = "stuff"; 
      console.log(sess.test); //prints
  }

}

0 个答案:

没有答案