Compound/Express JS:每个用户多个会话 [英] Compound/Express JS: Multiple sessions per user

查看:120
本文介绍了Compound/Express JS:每个用户多个会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为每个用户创建2个会话的应用程序.我已找到问题的根源,但我不完全了解为什么会发生这种情况以及如何解决它.假设我搭建了一个示例应用程序,如下所示:

I have an app that is creating 2 sessions for each user. I have located the source of the issue, but I do not fully understand why it's happening and how to fix it. Let's say I scaffold an example app like so:

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 文件中,对其进行修改以使其具有以下内容:

And in the app/controllers/mytests_controller.js file, modify it to have this:

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,但是也许我误会了其他东西吗?谁能解释发生了什么事?

When I navigate to localhost:3000/mytests, and crack open the Mongo database, I see 2 sessions created. If I uncomment that return in the index, I only get 1 session created, so it's clearly the http.get, but maybe I'm misunderstanding something else? Can anyone explain what's going on?

理想情况下,我只希望我浏览至/mytests进行会话,而不要随后进行任何呼叫.

Ideally, I just want me browsing to /mytests to make a session and not any subsequent calls it makes.

注意:我意识到这个示例非常愚蠢,/getMe端点仅返回一些JSON,但是在我的实际应用中,它做了更多的工作并进行了服务调用.

Note: I realize this example is pretty dumb with the /getMe endpoint just returning some JSON, but in my actual app, it's doing a bit more and making a service call.

CompoundJS 快速 Google网上论坛.

Cross-posted from the CompoundJS and Express Google Groups.

推荐答案

我不喜欢这个答案,但我认为我暂时会发布我的快速修复程序:

I don't like this answer, but I figured I'd post my quick fix for the time being:

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

I'm setting a credentials field in the user's session, so what I'm doing is just checking to see if credentials exists in the /getMe session. If it doesn't, delete the session. Here's the updated code:

app/controllers/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不创建会话,很棒.

Like I said, it's not ideal, so I would love to hear other answers... maybe if there's a way to send a session through an http.get or even telling the http.get to not create a session would be awesome.

这篇关于Compound/Express JS:每个用户多个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆