title: Can't set headers after they are sent: Error
body: <p>In reference to the similar question 
  <a href="https://stackoverflow.com/questions/7042340/node-js-error-cant-set-headers-after-they-are-sent">Error: Can&#39;t set headers after they are sent to the client</a></p>
  
  <p>I was getting a similar error and was able to resolve it by adding</p>
  
  <pre><code>return res.redirect(.....);
  next()
  </code></pre>
  
  <p>Could some one explain how this works? Can't seem to understand why this resolves the issue</p>
  
  <p>Edit:<strong>Adding more the question</strong></p>
  
  <p>So i have the following route set up </p>
  
  <pre><code>router.get('/register', userDetail, auth.authorize({
      failureRedirect: '/login/failure'
  }));
  </code></pre>
  
  <p>And under my <strong>userDetail.js</strong></p>
  
  <pre><code>if(req.securityContext.actor.auth_state !== "LOGGEDIN"){
      return res.redirect(unifiedLoginUrl);
      //next();
  }
  </code></pre>
  
  <p>The above code results in an error, can'set headers after sent </p>