title: How to handle ajax post request with node.js and without framworks?
body: <p>I'm practicing node js and ajax without any frameworks (like jquery, expressJS) by doing a simple app that gives information about a city's weather using openweatherapi.
  Here is my code so far:
      app.js</p>
  
  <pre><code>    const http = require("http");
      const fs = require("fs");
      const path = require("path");
      const { parse } = require('querystring');
  
  
      const server = http.createServer((req, res)=&gt;{
      if(req.url === "/"){
           fs.readFile("index.html", "UTF-8", function(err, data){
           res.writeHead(200, {"Content-Type": "text/html"});
           res.end(data);
           });
       }else if(req.url === "/public/styles.css"){
           var cssPath = path.join(__dirname + req.url);
           var cssFileStream = fs.createReadStream(cssPath, "UTF-8");
           res.writeHead(200, {"Content-Type": "text/css"});
           cssFileStream.pipe(res);
       }else if(req.url === "/public/main.js"){
           var jsFileStream = fs.createReadStream(`${__dirname}/${req.url}`, "UTF-8");
           res.writeHead(200, {"Content-Type": "text/js"});
           jsFileStream.pipe(res);
       }else if(req.url === "/favicon.ico"){
           res.statusCode=204;
           res.end();
      };
       if(req.url ==="/"&amp;&amp;req.method==="POST"){
           let body = "";
           req.on('data', chunk=&gt;{
               body += chunk.toString();
           });
           req.on("end", ()=&gt;{
               parse(body);
           });
           console.log(body);
       };
  
    });
  
    var PORT = process.env.port || 3000;
    server.listen(PORT);
    console.log(`Server listening on port ${PORT}`);
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Weather Application&lt;/title&gt;
          &lt;link href="./public/styles.css" rel="stylesheet" type="text/css"/&gt;
          &lt;script src="./public/main.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div class="weather-div"&gt;
              &lt;h1&gt; Search for weather information of a city&lt;/h1&gt;
              &lt;form method="post" action="/"&gt;
              &lt;input class="locationName" id="cityName" name="city" type="text" placeholder="City" required/&gt;
              &lt;input class="locationName" id="countryName" name="city" type="text" placeholder="Country"/&gt;
              &lt;/form&gt;
              &lt;button id="submitBtn" type="submit"&gt;Search Weather&lt;/button&gt;
          &lt;/div&gt;
      &lt;body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>main.js</p>
  
  <pre><code>function getData(){
      var city = document.getElementById('cityName');
      var country = document.getElementById('countryName');
  
      if(city.value.length&gt;0){
      const apiKey = "APIKey";
      const apiUrl =  "http://api.openweathermap.org";
  
      const xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
          if (this.readyState == 4 &amp;&amp; this.status == 200) {
  
          };
      };
      xhttp.open("POST", "app.js",true);
      xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xhttp.send(`city=${city}&amp;country=${country}`);
      };
  };
  
  window.onload=function(){
      document.getElementById("submitBtn").addEventListener("click", getData, false);
  };
  </code></pre>
  
  <p>So what i want to do is to send the city name of the input using ajax because i tried with a simple form and a submit button, but it keeps on refreshing the page. I don't want it. And I want a receive the data in app.js to parse it and filter its code with a json file of cities and then return it to main.js to send an api call to openweathermap.
  Fortunately i know how to do the simple stuff: the parsing and the api call. But all the other stuff i have totally no idea. when i search about it, I only find a solution using jquery or express, but I don't want that, i want pure javascript to get better.
  Thank you in advance. </p>