2

I have tried to find an answer to my problem but I have had no luck. I apologize in case this has already been answered.

I am trying to write a web service with Express.js. Here is my code:

var express = require('express');
var app = express();

app.get('/getgraph', function(req, res) {
    var d3 = require('d3'); 

    // Creating svg stub
    var graphContainer = d3.select("body")
        .append("svg") 
        .attr("id", "graph");
 ...*rest of the code*...
}

The problem is that when I call my service from two different browsers at the same time, two svg containers are added. What, I want is that each request gets it's own elements. How can I achieve this? I know that JavaScript is single threaded, but I would guess that d3 is in the scope of a request not in the scope of the server.

Thank you!

hexacyanide
  • 88,222
  • 31
  • 159
  • 162

3 Answers3

0

What happens when you move d3 to be in the scope of the server?

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
  • It behaves the same. In both cases it looks like all client-to-server calls share the same d3 object. – user2771953 Sep 13 '13 at 01:12
  • Also this: when I call the server from the same browser two (or more) times in a row, d3 object stays the same, therefore two (or more) svg elements are added. When I manually remove previously added svg elements, other calls also get responses with removed svg elements (which is wrong). – user2771953 Sep 13 '13 at 01:22
0

d3 should be in the client side code javascript code. Just the data should be passed from the server so that the client browser can render the data. The answer to this question should get you on the right path.

Community
  • 1
  • 1
timohare
  • 31
  • 3
0

In your app.js file include D3js like this:

var d3js = require('d3');

In other words don't try to require D3js from inside your request handler.

You can install d3 via npm with:

npm install d3

Also you may want to update your package.json to include D3js as a dependency

Otto Borden
  • 133
  • 3
  • 15