title: Accessing Express Session Variables in views
body: <p>I am trying to access the session variables in EJS views. But not able to do that
  Below is the code</p>
  
  <pre><code>var express=require('express');
  var mongoose=require('mongoose');
  var db=require('./models/db.js');
  
  var routes=require('./routes/route.js');
  var user=require('./routes/user.js');
  var bodyParser=require('body-parser');
  
  var session=require('express-session');
  var cookieParser=require('cookie-parser');
  
  mongoose.connect('mongodb://localhost/test');
  
  var app=express();
  
  app.set('view engine','ejs');
  
  app.use(express.static(__dirname + '/public'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended:false}));
  app.use(cookieParser());
  app.use(session({secret:"qazwsxedcrfvtgbyhnujm"}));
  
  
  app.get('/',routes.home);
  app.get('/register',routes.register);
  app.post('/createUser',user.doCreate);
  app.get('/login',routes.login);
  app.post('/authenticate',user.login);
  
  
  var port = process.env.PORT || 3000;
  
  var server=app.listen(port,function(req,res){
      console.log("Catch the action at http://localhost:"+port);
  });
  </code></pre>
  
  <p>Here is the login handler</p>
  
  <pre><code>    exports.login=function(req,res){
  
      var username=req.body.username;
      var password=req.body.password;
  
      User.findOne({username:username}, function(err,user){
  
        if(err){
          console.log("Error : Please enetr valid username and password");
          res.redirect("/login");
        }
  
       user.comparePassword(password,function(err,isMatch){
         if(isMatch &amp;&amp; isMatch==true){
           console.log("Authentication Suucessfull");
           req.session.username=user.username;
           res.render("/",{username:req.session.username});
         }else{
           console.log("Authentication UnSuucessfull");
           res.redirect("/login");
         }
  
       });
  
      });
    }
  </code></pre>
  
  <p>In the EJS view I am trying to print the username that is set by the login handler</p>
  
  <pre><code>&lt;div id="page-content-wrapper"&gt;
              &lt;div class="container-fluid"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-lg-12"&gt;
                          &lt;h1&gt;Awesome&lt;/h1&gt;
                          &lt;% if( typeof(username) !== 'undefined' ) { %&gt;
                          &lt;h1&gt;&lt;%=username%&gt;&lt;/h1&gt;
                          &lt;%}%&gt;
                          &lt;p&gt;This template has a responsive menu toggling system. The menu will appear collapsed on smaller screens, and will appear non-collapsed on larger screens. When toggled using the button below, the menu will appear/disappear. On small screens, the page content will be pushed off canvas.&lt;/p&gt;
                          &lt;p&gt;Make sure to keep all page content within the &lt;code&gt;#page-content-wrapper&lt;/code&gt;.&lt;/p&gt;
                          &lt;a href="#menu-toggle" class="btn btn-default" id="menu-toggle"&gt;Toggle Menu&lt;/a&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>What I am missing here?</p>