Compound / Express JS:每个用户多个会话

时间:2014-01-27 21:32:48

标签: node.js session express compoundjs railway.js

我有一个为每个用户创建2个会话的应用。我找到了问题的根源,但我不完全理解它为什么会发生以及如何解决它。让我们说我支持一个像这样的示例应用程序:

compound init blah
cd blah
npm install
npm install connect-mongo
compound g c mytest

config / environment.js 看起来像这样:

module.exports = function (compound) {
  var express = require('express');
  var app = compound.app;
  var secret = 'secret';  // Need this to check if there's been tampering with the session
  var MongoStore = require('connect-mongo')(express);
  app.sessionStore = new MongoStore({
    url: 'mongodb://localhost/development'
  });
  app.cookieParser = express.cookieParser(secret);

  app.configure(function() {
    app.use(express.static(app.root + '/public', { maxAge: 86400000 }));
    app.set('jsDirectory', '/javascripts/');
    app.set('cssDirectory', '/stylesheets/');
    app.set('cssEngine', 'stylus');
    compound.loadConfigs(__dirname);
    app.use(express.bodyParser());
    app.use(app.cookieParser);
    app.use(express.session({
      secret: secret,
      store: app.sessionStore,
      cookie: {
        maxAge: 86400000 // 24 hour session
      }
    }));
    app.use(express.methodOverride());
    app.use(app.router);
  });
};

app / controllers / mytests_controller.js 文件中,修改它以使其具有:

action('getMe', function(data) {
  return send({success: true, data: 'got you!'});
});

action(function index(data) {
  console.log(data.req.session);  // has session data
  var http = require('http');
  var options = {
    host: 'localhost',
    port: 3000,
    path: '/getMe'
  };
  //return send({success: true});
  http.get(options, function(res) {
    var data = '';
    res.on('data', function(chunk) {
      data += chunk;
    });
    res.on('end', function() {
      data = JSON.parse(data);
      if (data) {
        return send({success: true, data: data});
      }
      else {
        return send({success: false, data: 'data is undefined'});
      }
    });
  });
});

更新 routes.js

exports.routes = function (map) {
    map.resources('mytests');
    map.get('getMe', 'mytests#getMe');

    // Generic routes. Add all your routes below this line
    // feel free to remove generic routes
    map.all(':controller/:action');
    map.all(':controller/:action/:id');
};

当我导航到 localhost:3000 / mytests ,并打开Mongo数据库时,我看到创建了2个会话。如果我在索引中取消注释该返回,我只创建了1个会话,所以它显然是http.get,但也许我误解了别的东西?谁能解释一下发生了什么?

理想情况下,我只是希望我浏览/ mytests进行会话而不是后续调用。

注意:我意识到这个例子对于/ getMe端点只是返回一些JSON非常愚蠢,但在我的实际应用中,它做了一些更多的事情并进行了服务调用。

CompoundJSExpress Google网上论坛交叉发布。

1 个答案:

答案 0 :(得分:0)

我不喜欢这个答案,但我想我暂时会发布我的快速解决方案:

我在用户的会话中设置了credentials字段,所以我正在做的只是检查/ getMe会话中是否存在credentials。如果没有,请删除会话。这是更新后的代码:

应用/控制器/ mytests_controller.js

action('getMe', function(data) {
  // Added this part
  var session = data.req.session;
  if (session && !session.credentials) {
    session.destroy();
  }

  return send({success: true, data: 'got you!'});
});

action(function index(data) {
  // Added this part
  var session = data.req.session;
  if (session) {
    session.credentials = "blah blah blah";
  }

  var http = require('http');
  var options = {
    path: '/getMe',
    host: 'localhost',
    port: 3000
  };
//  return send({success: true});
  http.get(options, function(res) {
    var data = '';
    res.on('data', function(chunk) {
      data += chunk;
    });
    res.on('end', function() {
      data = JSON.parse(data);
      if (data) {
        // make call for proper config file based on cookie.role
        return send({success: true, data: data});
      }
      else {
        return send({success: false, data: 'data is undefined'});
      }
    });
  });
});

就像我说的那样,它并不理想,所以我很乐意听到其他答案...也许如果有办法通过http.get发送会话,或者甚至告诉http.get不创建会话将是真棒。