title: ExpressJS : res.redirect() not working as expected?
body: <p>I've been struggling for 2 days on this one, googled and stackoverflowed all I could, but I can't work it out.</p>
  
  <p>I'm building a simple node app (+Express + Mongoose) with a login page that redirects to the home page. Here's my server JS code :</p>
  
  <pre><code>app
      .get('/', (req, res) =&gt; {
          console.log("Here we are : root");
          return res.sendfile(__dirname + '/index.html');
      })
      .get('/login', (req, res) =&gt; {
          console.log("Here we are : '/login'");
          return res.sendfile(__dirname + '/login.html');
      })
      .post('/credentials', (req, res) =&gt; {
          console.log("Here we are : '/credentials'");
          // Some Mongoose / DB validations
          return res.redirect('/');
      });
  </code></pre>
  
  <p>The login page makes a POST request to <code>/credentials</code>, where posted data is verified. This works. I can see "Here we are : '/credentials'" in the Node console.</p>
  
  <p>Then comes the issue : the res.redirect doesn't work properly. I know that it does reach the '/' route, because :</p>
  
  <ol>
  <li>I can see "Here we are : root" in the Node console</li>
  <li><strong>The index.html page is being sent back to the browser as a reponse, but not displayed in the window.
  Chrome inspector shows the POST request response, I CAN see the HTML code being sent to the browser in the inspector, but the URL remains /login and the login page is still being displayed on screen.</strong></li>
  </ol>
  
  <p>(Edit) The redirection is in Mongoose's callback function, it's not synchronous (as NodeJS should be). I have just removed Mongoose validation stuff for clarity.</p>
  
  <p>I have tried adding <code>res.end()</code>, doesn't work</p>
  
  <p>I have tried</p>
  
  <pre><code>req.method = 'get'; 
  res.redirect('/');
  </code></pre>
  
  <p>and</p>
  
  <pre><code>res.writeHead(302, {location: '/'});
  res.end();
  </code></pre>
  
  <p>Doesn't work</p>
  
  <p>What am I doing wrong? How can I actually leave the '/login' page, redirect the browser to '/' and display the HTML code that it received?</p>
  
  <p>Thanks a million for your help in advance :)</p>