Wednesday 24 May 2017

Node.js express chunked response not encoded with new line `\r\n` after every chunk

Liked title described, I tried to use chunked response (multiple res.write()) in Node.js with Express:

res.header('Transfer-Encoding', 'chunked');

// Different JSON object arrived at different time,
// so I cannot or I don't want to combine them at certain point...
res.write(JSON.stringify(jsonObjA));
res.write(JSON.stringify(jsonObjB));
res.write(JSON.stringify(jsonObjC));

res.end();

Then the http client I am using is axios, which the response I got is just simply combined big string, there is no \r\n appended after each chunk like described here: Transfer-Encoding: chunked, so I have no way to parse the response.

So:

  1. Where I am doing wrongly?
  2. Or I need to append \r\n manually after each chunk?
  3. How to do the response.on('data', () => {}) so I can listen on each chunk in axios?


via lnshi

No comments:

Post a Comment