title: Heroku Nodejs Webhosting
body: <p>Im trying to host a election website.</p>
  <p>My Current Method explained:
  There is server.js file with serves the public file. If its a POST request(sent by frontend.js to send data to server) edits the poll.json file based on the data sent.
  I have a public file having index.html, frontend.js, poll.json where poll.json stores all the data.</p>
  <p>My current code work all well in my localhost.
  But when running it in Heroku I get a error in the POST request line saying 'POST http://localhost:53390/ net::ERR_CONNECTION_REFUSED1
  *********.herokuapp.com/:1 Uncaught (in promise) TypeError: Failed to fetch'</p>
  <p>My server.js code:</p>
  <pre><code>const createServer = require('http').createServer;
  const express = require('express');
  const app = express();
  let pollData = require(__dirname+'/public/poll.json');
  const fs = require('fs');
  var all_usn_no=[];
  const listening_port=process.env.PORT || 4000;
  
  function get_roll_and_usn(pollData){
    for(var i=0;i&lt;=pollData.students.length-1;i++){
        //all_roll_no.push(pollData.students[i][0]);
        all_usn_no.push(pollData.students[i][1]);
    }
  }
  function roll_to_row(in_usn){
    get_roll_and_usn(pollData)
    return all_usn_no.indexOf(in_usn);
  }
  function write_vote(votes){
    var checking_row=roll_to_row(votes[1]);
    pollData.students[checking_row]=votes;
    fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) =&gt; {
        if (err) throw err;
        console.log('Data written to file');
    });
  }
  
  write_vote([listening_port,0]);
  
  app.use(express.static(__dirname+'/public'));
  app.get('/', (req, res) =&gt; {
      res.sendFile(__dirname);// + '/index.html');
  });
  app.post('/', (req, res) =&gt; {
      let body = '';
      req.on('data', data =&gt; body += data)
      req.on('end', () =&gt; {
        res.writeHead(200, {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'POST, GET',
        });
        body=body.split(&quot;,&quot;);
        console.log(body.toString());
        write_vote(body);
        res.end(`{ &quot;response&quot;: &quot;${body ? body : 'No body sent' }&quot; }`);
      })
  });
  app.listen(listening_port, () =&gt; {
    console.log('Example app listening at http://localhost:'+listening_port)
  });
  </code></pre>
  <p>my frontend.js program:</p>
  <pre><code>        var roll='';
      var client_ip='';
      var usn='';
      var date='';
      const all_roll_no=[]
      const all_usn_no=[]
      var port='';
  
      function check(roll,usn){
          var data='';
          const url='poll.json';
          const Http = new XMLHttpRequest();
          Http.open(&quot;GET&quot;, url);
          Http.send();
          Http.onload = () =&gt;{
              data=JSON.parse(Http.response);
              get_roll_and_usn(data);
              check_validity(roll,usn,data);
          }
      }
      function get_roll_and_usn(pollData){
          for(var i=0;i&lt;=pollData.students.length-1;i++){
              //all_roll_no.push(pollData.students[i][0]);
              all_usn_no.push(pollData.students[i][1]);
          }
      }
      function usn_to_row(in_usn){
          return all_usn_no.indexOf(in_usn);
      }
      function check_validity(checking_roll,checking_usn,data){
          var checking_row=usn_to_row(checking_usn);
          port=data.students[0][0];
          //if(all_roll_no.indexOf(checking_roll)&gt;=0 &amp;&amp; checking_usn==all_usn_no[all_roll_no.indexOf(checking_roll)] &amp;&amp; data.students[checking_row][2]==0){
          if(all_usn_no.indexOf(checking_usn)&gt;=0 &amp;&amp; data.students[checking_row][2]==0){
              //console.log(&quot;valid&quot;);
              document.getElementById(&quot;page_2&quot;).style.display = &quot;none&quot;;
              document.getElementById(&quot;page_3&quot;).style.display = &quot;block&quot;;
              fetch('https://api.ipify.org/?format=json').then(results=&gt; results.json()).then(data =&gt; client_ip=data.ip);
          }
          else{
              alert(&quot;You cannot vote/ You have already voted&quot;)
              //console.log('invalid');
          }
      }
  
  
      document.getElementById(&quot;startbutton&quot;).onclick = function(){ 
          roll = document.getElementById(&quot;roll_no&quot;).value;
          usn = document.getElementById(&quot;usn_no&quot;).value;
          date = Date();
          check(roll,usn);
      } 
      document.getElementById(&quot;next&quot;).onclick = function() {    
          document.getElementById(&quot;page_1&quot;).style.display = &quot;none&quot;;
          document.getElementById(&quot;page_2&quot;).style.display = &quot;block&quot;;
      }
      document.getElementById(&quot;finish&quot;).onclick = function() {
          var splb=[document.getElementById(&quot;splb1&quot;).checked,document.getElementById(&quot;splb2&quot;).checked,document.getElementById(&quot;splb3&quot;).checked,document.getElementById(&quot;splb4&quot;).checked,document.getElementById(&quot;splb5&quot;).checked,document.getElementById(&quot;splb6&quot;).checked,document.getElementById(&quot;splb7&quot;).checked];
          var splg=[document.getElementById(&quot;splg1&quot;).checked,document.getElementById(&quot;splg2&quot;).checked,document.getElementById(&quot;splg3&quot;).checked,document.getElementById(&quot;splg4&quot;).checked,document.getElementById(&quot;splg5&quot;).checked,document.getElementById(&quot;splg6&quot;).checked,document.getElementById(&quot;splg7&quot;).checked];
          var asplb=[document.getElementById(&quot;asplb1&quot;).checked,document.getElementById(&quot;asplb2&quot;).checked,document.getElementById(&quot;asplb3&quot;).checked,document.getElementById(&quot;asplb4&quot;).checked,document.getElementById(&quot;asplb5&quot;).checked,document.getElementById(&quot;asplb6&quot;).checked,document.getElementById(&quot;asplb7&quot;).checked];
          var asplg=[document.getElementById(&quot;asplg1&quot;).checked,document.getElementById(&quot;asplg2&quot;).checked,document.getElementById(&quot;asplg3&quot;).checked,document.getElementById(&quot;asplg4&quot;).checked,document.getElementById(&quot;asplg5&quot;).checked,document.getElementById(&quot;asplg6&quot;).checked,document.getElementById(&quot;asplg7&quot;).checked];
          var csb=[document.getElementById(&quot;csb1&quot;).checked,document.getElementById(&quot;csb2&quot;).checked,document.getElementById(&quot;csb3&quot;).checked,document.getElementById(&quot;csb4&quot;).checked,document.getElementById(&quot;csb5&quot;).checked,document.getElementById(&quot;csb6&quot;).checked,document.getElementById(&quot;csb7&quot;).checked];
          var csg=[document.getElementById(&quot;csg1&quot;).checked,document.getElementById(&quot;csg2&quot;).checked,document.getElementById(&quot;csg3&quot;).checked,document.getElementById(&quot;csg4&quot;).checked,document.getElementById(&quot;csg5&quot;).checked,document.getElementById(&quot;csg6&quot;).checked,document.getElementById(&quot;csg7&quot;).checked];
          var acsb=[document.getElementById(&quot;acsb1&quot;).checked,document.getElementById(&quot;acsb2&quot;).checked,document.getElementById(&quot;acsb3&quot;).checked,document.getElementById(&quot;acsb4&quot;).checked,document.getElementById(&quot;acsb5&quot;).checked,document.getElementById(&quot;acsb6&quot;).checked,document.getElementById(&quot;acsb7&quot;).checked];
          var acsg=[document.getElementById(&quot;acsg1&quot;).checked,document.getElementById(&quot;acsg2&quot;).checked,document.getElementById(&quot;acsg3&quot;).checked,document.getElementById(&quot;acsg4&quot;).checked,document.getElementById(&quot;acsg5&quot;).checked,document.getElementById(&quot;acsg6&quot;).checked,document.getElementById(&quot;acsg7&quot;).checked];
          var scb=[document.getElementById(&quot;scb1&quot;).checked,document.getElementById(&quot;scb2&quot;).checked,document.getElementById(&quot;scb3&quot;).checked,document.getElementById(&quot;scb4&quot;).checked,document.getElementById(&quot;scb5&quot;).checked,document.getElementById(&quot;scb6&quot;).checked,document.getElementById(&quot;scb7&quot;).checked];
          var scg=[document.getElementById(&quot;scg1&quot;).checked,document.getElementById(&quot;scg2&quot;).checked,document.getElementById(&quot;scg3&quot;).checked,document.getElementById(&quot;scg4&quot;).checked,document.getElementById(&quot;scg5&quot;).checked,document.getElementById(&quot;scg6&quot;).checked,document.getElementById(&quot;scg7&quot;).checked];
          var ascb=[document.getElementById(&quot;ascb1&quot;).checked,document.getElementById(&quot;ascb2&quot;).checked,document.getElementById(&quot;ascb3&quot;).checked,document.getElementById(&quot;ascb4&quot;).checked,document.getElementById(&quot;ascb5&quot;).checked,document.getElementById(&quot;ascb6&quot;).checked,document.getElementById(&quot;ascb7&quot;).checked];
          var ascg=[document.getElementById(&quot;ascg1&quot;).checked,document.getElementById(&quot;ascg2&quot;).checked,document.getElementById(&quot;ascg3&quot;).checked,document.getElementById(&quot;ascg4&quot;).checked,document.getElementById(&quot;ascg5&quot;).checked,document.getElementById(&quot;ascg6&quot;).checked,document.getElementById(&quot;ascg7&quot;).checked];
          var vote=[String(splb.indexOf(true)),String(splg.indexOf(true)),String(asplb.indexOf(true)),String(asplg.indexOf(true)),String(csb.indexOf(true)),String(csg.indexOf(true)),String(acsb.indexOf(true)),String(acsg.indexOf(true)),String(scb.indexOf(true)),String(scg.indexOf(true)),String(ascb.indexOf(true)),String(ascg.indexOf(true))]
          var update=[String(roll),String(usn),&quot;1&quot;,String(client_ip),String(date)].concat(vote);
  
          if (update.indexOf(&quot;-1&quot;)&lt;0){
              alert(&quot;Pls vote for all posts&quot;)
          }
          else{
              document.getElementById(&quot;page_1&quot;).style.display = &quot;none&quot;;
              document.getElementById(&quot;page_2&quot;).style.display = &quot;none&quot;;
              document.getElementById(&quot;page_3&quot;).style.display = &quot;none&quot;;
              fetch('http://localhost:'+port,{method:'POST',body:update}).then(results=&gt; results.json()).then(console.log);
              //console.log(update);
  
              alert(&quot;Your vote has been registered&quot;)
          }    
      } 
  </code></pre>
  <p>You can just ignore the function as they are just to process the data and do necessary funtions.</p>
  <p>Main problem: PORT request from frontend.js to server.js sending data to edit the poll.json file return error.</p>
  <p>Thanks in advance.</p>