title: Node js too many redirect using middleware redirect
body: <p>In my Node.js application (I'm using express 4.x) I want to check if the user is logged. If the user isn't logged I want to redirect to my login page. Then I do that in the middleware like this :</p>
  
  <p><strong>Server.js</strong></p>
  
  <pre><code>app.use(function (req, res, next) {
  
      // if user is authenticated in the session, carry on
      if (req.isAuthenticated())
          return next();
  
      // if they aren't redirect them to the home page
      res.redirect('/login');
  });
  </code></pre>
  
  <p><strong>Login route</strong></p>
  
  <pre><code>// Login page
  app.get('/login', function(req, res){
      res.render('pages/login', {
                  error   : req.flash('loginError'),
                  info    : req.flash('info'),
                  success : req.flash('success')
              });
  });
  </code></pre>
  
  <p>But when I add this code in my middleware, the login page is called more than 30 times... And my browser says <code>Too many redirect</code>.</p>
  
  <p>Do you know why my login page is called a lot ?</p>