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.

2
  • 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. Commented May 17, 2018 at 1:16
  • I think this could help you: github.com/rvagg/through2 Commented May 17, 2018 at 1:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.