0

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.

csdev
  • 323
  • 2
  • 10
  • I ended up writing to a large from xmlbuilder, then streaming the response by piping res to the file with fs.createReadStream("./bigxmlfile.xml").pipe(res), as that works as well though requires waiting till the file is written. I'm leaving this open in case anyone can answer how to do it as the data chunks. – csdev May 17 '18 at 01:16
  • I think this could help you: https://github.com/rvagg/through2 – Lucas David Ferrero May 17 '18 at 01:26

0 Answers0