0

I'm developping a one-page application at data-vizualisation purpose, using D3. The fact is that I have a lot of data to transfer from my server (which is Express) to my client (several Mo in json).

I'd like to test something but i don't if this can be done : i'd like to dynamically generate some html code (for instance the content of a div) on my server and be able to send it to the client.

Can this be done, and if so, how ?

MrFlo
  • 309
  • 1
  • 5
  • 14

2 Answers2

1

If you want to generate with GET :

app.get('/', function (req, res) {
  var html = "<h1>Hello world</h1";
  res.send(html)
});
  • i'm not sure this will do the job... let's say i do what you suggest, then how do i retrieve this on my client side, and above all how do i insert it in my existing html code ? – MrFlo Jan 13 '17 at 16:47
0

Going off Jean's answer, if you have your server set up to respond to a GET request like he showed in this code:

app.get('/', function (req, res) {
  var html = "<h1>Hello world</h1";
  res.send(html)
});

Then you would send the request from your client side to your server URL with javascript like shown in the httpGetAsync function in this answer.

Then the callback will receive the response from your server, and you can insert your generated content from the response into your existing html code by accessing your DOM elements with javascript or D3 if you are already using it.

Community
  • 1
  • 1
haydenwagner
  • 546
  • 2
  • 6
  • 15
  • (sorry for late answer...) oh you might be right, something like d3.html(response) ! All simply ! I gonna try, thanks. – MrFlo Jan 23 '17 at 11:00
  • Yes exactly, 'd3.select('#test').html(response)' will set the inner html of a page element with the id='test' equal to the response text. – haydenwagner Jan 25 '17 at 02:22