how to stream a large xml response from express

987 Views Asked by At

I am trying to stream a large xml file from express to a client, and I have not yet found out how to send until the file is done processing on the server, and res.end() is called.

The xml file is build with xmlbuilder-js. It has a callback that receives document chunks, in which I am trying to send using response.write(chunk).

res.writeHead(200, {
    'Content-Type': 'text/xml',
    'Transfer-Encoding': 'chunked'
  })

   xmlToReturn = xmlBuilder.begin({
      writer: {
        pretty: true,
      }
    }, function(chunk) { 
      res.write(chunk)
    }).dec('1.0', 'UTF-8', true)
...
res.end()

The callback works as expected, it shows the data chunks coming through.

I have tried:

  • changing the content-type on the response to, for example, 'application/octet-stream'
  • using res.flush() after calling res.write(), or doing that periodically
  • experimenting with other headers

In all cases, if I can get the response to send, the client never receives the start of it until res.end() is called. What do I need to do so that express starts delivering the content as it flows through the callback?

I've explored questions and posts like this, which suggest my approach is correct but I am doing something wrong, or streaming is not working in express possibly due to other modules or middleware.

0

There are 0 best solutions below