title: Another express.js req.session not storing data
body: <p>I have been staring at the same SO questions/answers for too long.</p>
  
  <p>I am trying to set my user ID in a req.session variable</p>
  
  <p>here is my app.js stack</p>
  
  <pre><code> /**
   * Module dependencies.
   */
  
   var express = require('express')
    , routes = require('./routes')
    , cors = require('cors')
    , mongoose = require('mongoose')
    , User = require('./user-model')
    , path = require('path')
    , mysql = require('mysql');
  
  app = express()
  /**
   * Middleware.
   */
  app.use(cors());
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'supercalafragalistic' }));
  app.locals.pretty = true;
  app.use(express.static(path.join(__dirname, 'public')));
  app.set('view engine', 'jade');
  ...
  app.get('/someRoute', routes.someRoute)
  </code></pre>
  
  <p>I have moved the app.router from top to bottom. Currently it is implied (I just deleted it as I stripped my code looking for error). I have exported app, included app in routes (also erased) . I added middleware. I am lost.....</p>
  
  <p>here is my route stack</p>
  
  <pre><code>//routes/index.js
  //requires
  var  User = require('../user-model')
      ,qs = require('querystring')
      ,http = require('http')
      ,mysql = require('mysql')
      ,connection = mysql.createConnection({
           edited
      });
  
      /**
   * Set up application.
   */
  
      connection.connect(function(err){
          if(err) console.log('failed to connect to mysql because'+ err);
          else console.log('connected to mysql');
      })
  </code></pre>
  
  <p>I save the id in the login route:</p>
  
  <pre><code>//login processing route
  exports.logIn = function(req, res, next){
      console.log('log in request from '+req.body.name);
      User.findOne({name:req.body.name}, function(err, user, next){
  
          if(err) return next(err);
          if(!user) return res.end();
  
  //check password
          user.comparePassword(req.body.password, function(err, isMatch){
              if(err) res.send('failed to find user');
              console.log(""+isMatch);
              if(isMatch){
                  console.log(user._id.toString()+' user loggd in');
                  //set session cookie
                  req.session.loggedIn = user._id.toString();
                  console.log('req.session.loggedIn set to :'+req.session.loggedIn );
                   res.send(user);
  
              }else {
                 res.send('User not found try again'); 
              }
  
          });    
      });
  </code></pre>
  
  <p>However, in all other routes the variable is undefined:</p>
  
  <pre><code>// clock status
  exports.clockStatus = function(req, res, next){
      var user = req.session.loggedIn;
      console.log('Status route, userID: '+user);
      if(user){
  </code></pre>
  
  <p>I found the answer in <a href="https://stackoverflow.com/questions/21212828/cannot-access-req-session-variables-in-express-nodejs?rq=1">"Cannot access req.session variables in Express/NodeJS"</a>  but the last part trails off:</p>
  
  <blockquote>
    <p>In your case, you have assigned req.session.user = user; only in
    /login request. It will not be available for further requests(/auth).</p>
    
    <p>You have to get user information in /auth request too by session id.
    (Or) Better you can use passport for authentication.</p>
  </blockquote>
  
  <p>HOW? How do you "get user information in /auth request too by session id"?</p>
  
  <p>Please ask for anything else you need to straighten me out.
  I'm stumped. I have tried some dumb stuff already!</p>