Monday 5 June 2017

Node Router with Ajax calls to update data on a webpage

I am trying upload data on the client side depending on the user inputs with Ajax calls, but I am not sure what is the best way to write the router.

Right now I have an index.html called by default url "/" in server.js,

app.get('/', callbackIndex);

function callbackIndex(req, res){
  res.sendFile('/index.html',{root: __dirname})
}

and in my index.html(I'm using browserfly by the way):

<body>

  <script src="https://cdn.plot.ly/plotly-1.2.0.min.js"></script>
  <div id="plot_1" style="width:600px;height:250px;"></div>
  <script src="bundle.js"></script>

</body>

in my bundle.js, I called an ajax function on url '/data', and my callback function will response with data and update the plot on "/":

      function callbackData(req, res){
       res.send(data)

}

    $.ajax({url: '/data', success: function(result){
      var arries =...
      redraw(x_array,y_array)
  }})

So far so good. Here's my question: If I want to add more endpoints, for example, the user type "/half_data" and only half of the data will be displayed on the page.

I tried to add endpoints in server.js', but when the server responses, it will send data back to '/half_data', not '/', so I can not redraw the plot with new data. Then I decided to send a html to '/half_data' and redraw it based on the new data, but each response can only send either data or html_file, but not both. Is there another way to finish this routher?

Thank you



via Deidara

No comments:

Post a Comment