title: Redirecting using "pure" Node.js from inside a callback function
body: <p>The following is an MCVE of my <code>server.js</code> code:</p>
  
  <pre><code>let fs = require('fs');
  let http = require('http');
  
  http.createServer((req, res) =&gt; {
      // Handles GET requests
      if(req.method == 'GET') {
          let file = req.url == '/' ? './index.html': '/login.html'; // just an example
          fs.readFile(file, (err, data) =&gt; {
              res.writeHead(200, {'Content-Type': 'text/html'});
              res.end(data);
          });
      } 
  
      // Handles POST requests
      else {
          read(status =&gt; {
              if(status) {
                  res.writeHead(302, {
                      'Location': 'http://localhost:8000/login.html',
                      'Content-Type': 'text/html'
                  });
                  res.end();
                  console.log('Redirected!');
              }
          });
      }
  }).listen(8000);
  
  // In my actual script, the `read` function reads JSON files and sends data,
  // so I've used the callback function
  let read = callback =&gt; fs.readFile( './index.html', (err, data) =&gt; callback(true) );
  </code></pre>
  
  <p>And, I've two HTML files as mentioned inside the code.</p>
  
  <p><code>index.html</code></p>
  
  <pre><code>&lt;input type="submit" onclick='let xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8000"); xhr.send();'&gt;
  </code></pre>
  
  <p><sup>I've used inline script to minimize traffic in my MCVE. For development purposes, I'll be using external scripts on my website</sup></p>
  
  <p><code>login.html</code></p>
  
  <pre><code>&lt;h1&gt;Login&lt;/h1&gt;
  </code></pre>
  
  <hr />
  
  <p>Now, when I open <code>http://localhost</code>, <code>index.html</code> shows up nicely. As you've noticed that <code>index.html</code> is just a button. So, when I click on that button, Ajax request is fired successfully and everything works fine (no console errors) EXCEPT the fact that page <strong>does not redirects</strong>. I don't know what it going wrong or what else is missing.</p>
  
  <p>I'm a beginner in Node.js and read about redirection in <a href="https://stackoverflow.com/questions/4062260/nodejs-redirect-url">Nodejs - Redirect url</a> and <a href="https://stackoverflow.com/questions/11355366/how-to-redirect-users-browser-url-to-a-different-page-in-nodejs">How to redirect user's browser URL to a different page in Nodejs?</a>, I've searched a lot but couldn't get a hint on this. Thanks for your time!</p>
  
  <blockquote>
    <p>Also, I'm aware about express, but I don't consider use of frameworks as they hide core concepts.</p>
  </blockquote>
  
  <hr />
  
  <p><strong>EDIT</strong>: When I try redirecting without the callback concept, then it works fine, as <a href="https://www.youtube.com/watch?v=neIeFVMsuJY" rel="noreferrer">this video</a> tells us.</p>