title: Compound/Express JS: Multiple sessions per user
body: <p>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:</p>
  
  <pre><code>compound init blah
  cd blah
  npm install
  npm install connect-mongo
  compound g c mytest
  </code></pre>
  
  <p>Make <strong>config/environment.js</strong> look like this:</p>
  
  <pre><code>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);
    });
  };
  </code></pre>
  
  <p>And in the <strong>app/controllers/mytests_controller.js</strong> file, modify it to have this:</p>
  
  <pre><code>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'});
        }
      });
    });
  });
  </code></pre>
  
  <p>Update <strong>routes.js</strong>:</p>
  
  <pre><code>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');
  };
  </code></pre>
  
  <p>When I navigate to <strong>localhost:3000/mytests</strong>, 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?</p>
  
  <p>Ideally, I just want me browsing to /mytests to make a session and not any subsequent calls it makes.</p>
  
  <p><strong>Note:</strong> 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.</p>
  
  <p>Cross-posted from the <a href="https://groups.google.com/forum/#!topic/compoundjs/r8tCMo-KbOc" rel="nofollow">CompoundJS</a> and <a href="https://groups.google.com/forum/#!topic/express-js/h7xmvv7rdTY" rel="nofollow">Express</a> Google Groups.</p>