title: Conditional app.use in node - express
body: <p>Is it possible to conditionally use <code>app.use</code> in app.js?
  Express <a href="https://github.com/expressjs/cookie-session" rel="nofollow">cookie-session</a> can <a href="https://github.com/expressjs/cookie-session/issues/21#issuecomment-52973886" rel="nofollow">not change the value of <code>maxAge</code> dynamically</a> and I was thinking of doing something of this kind, but I'm getting some errors:</p>
  
  <pre><code>app.use(function(req,res,next ){
    if(typeof req.session == 'undefined' || req.session.staySignedIn === 'undefined'){
      //removing cookie at the end of the session
      cookieSession({
        httpOnly: true,
        secure: false,
        secureProxy: true,
        keys: ['key1', 'key2']
      });
    }else{
      //removing cookie after 30 days
      cookieSession({
        maxAge: 30*24*60*60*1000, //30 days
        httpOnly: true,
        secure: false,
        secureProxy: true,
        keys: ['key1', 'key2']
      });
    }
    next();
  });
  </code></pre>
  
  <p>Instead of the normal use of it:</p>
  
  <pre><code>app.use(cookieSession({
    httpOnly: true,
    secure: false,
    secureProxy: true,
    keys: ['key1', 'key2']
  }));
  </code></pre>
  
  <p>Right now I'm getting the following error:</p>
  
  <blockquote>
    <p>Cannot read property 'user' of undefined</p>
  </blockquote>
  
  <p>I believe it is referring to this line (although it doesn't say where exactly)</p>
  
  <pre><code>req.session.user;
  </code></pre>