title: Use Ajax and Nodejs to update data on a page
body: <p>I am trying to write a loop to update a webpage every 30 seconds, but I am not sure how to make an ajax call with setInverval function. Here's my server code:</p>
  
  <pre><code>var app = express()
  app.get('/', function(req, res){
  // error here
  res.sendFile('./index.html',{root: __dirname})
  //...Some data 
  res.send(data)
  });
  </code></pre>
  
  <p>I have a setInverval function in my index.html:</p>
  
  <pre><code>&lt;script&gt;
  function ajaxCall(){
    $.ajax({url: "http://localhost:3000/", success: function(result){
      // I want to receive new result every 30 seconds
    }})
  
  }
  setInterval(ajaxCall,30000)
  &lt;/script&gt;
  </code></pre>
  
  <p>Since I am not sure how to deal with app.get("/") and the ajax request, I got</p>
  
  <pre><code>Error: Can't set headers after they are sent.
  </code></pre>
  
  <p>cause I tried to send data twice</p>
  
  <p>How should I modify the code so that I can see my data displaying on '<a href="http://localhost:3000/" rel="nofollow noreferrer">http://localhost:3000/</a>' and it updates every 30 seconds?</p>
  
  <p>Thank you.</p>