title: Should I use res.render or res.redirect with Express and EJS?
body: <p>I'm very new to this topic. I would like to learn more about when to use render and redirect. The goal is to get my user to another page after they click a button or log in. This new page needs data so I'm passing this data during the render command. This is working fine except that the page URL does not represent the correct page that they are currently viewing. This being the case I'm wondering if I should be doing a redirect. Below is my code.
  <strong>This is a snippet of code for my server.js page</strong></p>
  
  <pre><code>    // route for user Login
  app.route('/login')
      .get(sessionChecker, (req, res) =&gt; {
          res.render('login');
      })
      .post((req, res) =&gt; {
          var username = req.body.username,
              password = req.body.password;
  
          User.findOne({ where: { username: username } }).then(function (user) {
              if (!user) {
                  res.render('login');;
              } else if (!user.validPassword(password)) {
                  res.render('login');;
              } else {
                  req.session.user = user.dataValues;
                  res.render('dashboard',{
                      "token" :  createToken(req),
                      "user" : req.session.user
                   });
              }
          });
      });
  </code></pre>
  
  <p><strong>This is a snippet of code from my .ejs page</strong></p>
  
  <pre><code>&lt;% if( typeof user !== "undefined") { %&gt;
  
                      &lt;li&gt;&lt;a href="/dashboard"&gt;Dashboard&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="/reports"&gt;My Reports&lt;/a&gt;&lt;/li&gt;
                      &lt;li class="dropdown"&gt;
                          &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"&gt;Dropdown &lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
                          &lt;ul class="dropdown-menu"&gt;
                              &lt;li&gt;&lt;a href="#"&gt;Action&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;Another action&lt;/a&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;Something else here&lt;/a&gt;&lt;/li&gt;
                              &lt;li role="separator" class="divider"&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;Separated link&lt;/a&gt;&lt;/li&gt;
                              &lt;li role="separator" class="divider"&gt;&lt;/li&gt;
                              &lt;li&gt;&lt;a href="#"&gt;One more separated link&lt;/a&gt;&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
  
                      &lt;% } %&gt;
  </code></pre>
  
  <p>This is the full code from my server.js page.</p>
  
  <pre><code>var express = require('express');
  var bodyParser = require('body-parser');
  var cookieParser = require('cookie-parser');
  var session = require('express-session');
  var morgan = require('morgan');
  var User = require('./models/user');
  var crypto = require('crypto');
  
  // invoke an instance of express application.
  var app = express();
  
  // set our application port
  app.set('port', 9000);
  
  app.set('view engine', 'ejs');
  app.set('views', 'views');
  
  // set morgan to log info about our requests for development use.
  app.use(morgan('dev'));
  
  // initialize body-parser to parse incoming parameters requests to req.body
  app.use(bodyParser.urlencoded({ extended: true }));
  
  // initialize cookie-parser to allow us access the cookies stored in the browser. 
  app.use(cookieParser());
  
  // initialize express-session to allow us track the logged-in user across sessions.
  app.use(session({
      key: 'user_sid',
      secret: 'somerandonstuffs',
      resave: false,
      saveUninitialized: false,
      cookie: {
          expires: 600000
      }
  }));
  
  
  // This middleware will check if user's cookie is still saved in browser and user is not set, then automatically log the user out.
  // This usually happens when you stop your express server after login, your cookie still remains saved in the browser.
  app.use((req, res, next) =&gt; {
      if (req.cookies.user_sid &amp;&amp; !req.session.user) {
          res.clearCookie('user_sid');        
      }
      next();
  });
  
  
  // middleware function to check for logged-in users
  var sessionChecker = (req, res, next) =&gt; {
      if (req.session.user &amp;&amp; req.cookies.user_sid) {
          res.render(req.originalUrl.substr(1, req.originalUrl.length),{
              "token" :  createToken(req),
              "user" : req.session.user
           });
      } else {
          next();
      }    
  };
  
  // route for Home-Page
  app.get('/', sessionChecker, (req, res) =&gt; {
      res.redirect('/login');
  });
  
  
  // route for user signup
  app.route('/signup')
      .get(sessionChecker, (req, res) =&gt; {
          //res.sendFile(__dirname + '/public/signup.html');
          res.render('signup');
      })
      .post((req, res) =&gt; {
          User.create({
              username: req.body.username,
              email: req.body.email,
              password: req.body.password,
              roles: req.body.roles
          })
          .then(user =&gt; {
              req.session.user = user.dataValues;
              res.render('dashboard',{
                  "token" :  createToken(req),
                  "user" : req.session.user
               });
          })
          .catch(error =&gt; {
              res.render('signup');
          });
      });
  
  
  // route for user Login
  app.route('/login')
      .get(sessionChecker, (req, res) =&gt; {
          res.render('login');
      })
      .post((req, res) =&gt; {
          var username = req.body.username,
              password = req.body.password;
  
          User.findOne({ where: { username: username } }).then(function (user) {
              if (!user) {
                  res.redirect('/login');;
              } else if (!user.validPassword(password)) {
                  res.redirect('/login');;
              } else {
                  req.session.user = user.dataValues;
                  res.render('dashboard',{
                      "token" :  createToken(req),
                      "user" : req.session.user
                   });
              }
          });
      });
  
  
  // route for user's dashboard
  app.get('/dashboard', (req, res) =&gt; {
      if (req.session.user &amp;&amp; req.cookies.user_sid) {
  
          res.render('dashboard',{
             "token" : createToken(req),
             "user" : req.session.user
          });
      } else {
          res.render('login');
      }
  });
  
  // route for user's reports
  app.get('/reports', (req, res) =&gt; {
      if (req.session.user &amp;&amp; req.cookies.user_sid) {
  
          res.render('reports',{
             "token" : createToken(req),
             "user" : req.session.user
          });
      } else {
          res.render('login');
      }
  });
  
  // route for user's profile
  app.route('/user_profile')
  .get(sessionChecker, (req, res) =&gt; {
      res.render('user_profile');
  })
  .post((req, res) =&gt; {
      var username = req.body.username,
          password = req.body.password;
  
      User.findOne({ where: { username: username } }).then(function (user) {
          if (!user) {
              res.render('login');;
          } else if (!user.validPassword(password)) {
              res.render('login');;
          } else {
              req.session.user = user.dataValues;
              res.render('dashboard',{
                  "token" :  createToken(req),
                  "user" : req.session.user
               });
          }
      });
  });
  
  // route for user logout
  app.get('/logout', (req, res) =&gt; {
      if (req.session.user &amp;&amp; req.cookies.user_sid) {
          res.clearCookie('user_sid');
          res.render('login');;
      } else {
          res.render('login');;
      }
  });
  
  
  
  
  // route for handling 404 requests(unavailable routes)
  app.use(function (req, res, next) {
    res.status(404).send("Sorry can't find that!")
  });
  
  
  // start the express server
  app.listen(app.get('port'), () =&gt; console.log(`App started on port ${app.get('port')}`));
  
  function createToken(req){
      var data = 'u=' + req.session.user.username + "|o=organization_1";  
  
      if (req.session.user.roles.length &gt; 0){
          data += "|r=" + req.session.user.roles;
      }
  
      var buff = new Buffer(data);  
      var base64data = buff.toString('base64');
  
      console.log(base64data);  
  
      return base64data
  }
  </code></pre>