title: NodeJS/ExpressJS send response of large amount of data in 1 stream
body: <p>I'm prototyping an app using the native mongo rest api where Node returns about 400K of json. I use the following to maket he request to mongo's native api and return the result:</p>
  
  <pre><code>http.request(options, function(req)
    {
      req.on('data', function(data)
        {
  console.log(data,data.rows);
          response.send( 200, data );
        }
      );
    }
  )
  .on('error', function(error)
    {
  console.log('error\t',error);
      response.send(500, error);
    }
  )
  .end();
  </code></pre>
  
  <p>When I hit <code>http://localhost:8001/api/testdata</code> via curl, the response is proper (both what is outputted to Node's console from the <code>console.log</code> and what is received by curl). But when I hit it via ajax in my app, the stream is…interupted, even <code>data</code> outputted to Node's console (Terminal) is odd: It has multiple EOFs, and the Network > response for the call in chrome's dev tools ends at the first EOF.</p>
  
  <p>One other strange thing: <code>data</code> looks like:</p>
  
  <pre><code>{
      "offset": 0,
      "rows": [ … ]
  }
  </code></pre>
  
  <p>but in neither Node nor client-side (angular) can I reference data.rows (it returns undefined). <code>typeof data</code> returns <code>[object Object]</code>.</p>
  
  <p><strong>EDIT</strong> The request headers for both curl and angular (as reported by Node) are:</p>
  
  <pre><code>req.headers: {
    'x-action': '',
    'x-ns': 'test.headends',
    'content-type': 'text/plain;charset=utf-8',
    connection: 'close',
    'content-length': '419585'
  }
  </code></pre>
  
  <p><strong>EDIT</strong> I checked response headers in both angular and curl directly (instead of from Node), annnd there's a disagreement (same output from both curl and angular directly instead of from node):</p>
  
  <pre><code>access-control-allow-headers: "Origin, X-Requested-With, Content-Type, Accept"
  access-control-allow-methods: "OPTIONS,GET,POST,PUT,DELETE"
  access-control-allow-origin: "*"
  connection: "keep-alive"
  content-length: "65401" // &lt;---------------- too small!
  content-type: "application/octet-stream"
  //             ^-- if i force "application/json"
  // with response.json() instead of response.send() in Node,
  // the client displays octets (and it takes 8s instead of 0s)
  date: "Mon, 15 Jul 2013 18:36:50 GMT"
  etag: ""-207110537""
  x-powered-by: "Express"
  </code></pre>