Deidara Deidara - 21 days ago 12
Javascript Question

How to update front end data with node js?

I want to make a website that can display a plot with node.js. I also want to update the data every minute without refreshing the whole page. I am new to node.js and would like to learn how to use "get" request to update the data with a server.

Here I created a server in app.js:

http.createServer(function(req, res){
console.log('You are listening to port 3000');
fs.readFile('./controllers/index.html', function(err, data){
res.writeHead(200,{'Content-Type': 'text/html'});
res.write(data)
res.end();
});
}).listen(3000);


If my index.html is:

<html>
<body>
<h1>My Datasets</h1>
<p>Some data plot</p>
</body>
</html>


and I want to change the data plot every minute. How do I make a request from the frontend and provide the required data with the server?

Thanks.

Answer Source

You need to do two things:

  1. Create a route on your node server which returns the desired data.
  2. Perform an Ajax Request from the Client to fetch the data from the server and display your data accordingly. (In This case you would peform this request every minute)