Home > OS >  ExpressJs HTTP Streaming make me run out of disk space
ExpressJs HTTP Streaming make me run out of disk space

Time:06-18

My express server

app.get('/data', (_, res) => {
    const interval = setInterval(
        () => res.write(`${Math.random().toString()}\n`),
        1000
    );
    res.on('close', () => {
        clearInterval(interval);
        res.end();
    });
});

My client-side

const res = await fetch('/data')
const reader = res.body.getReader()
reader.read().then(data => {
    const { value, done} = data;
    console.log(value);
})

Everything working fine, data is streaming every second and log. However, whenever I refresh page or close it, the server-side start to filling up my disk space at rate of 100MB/second.

Best of my guess is that one single random number can't match with 100MB.

CodePudding user response:

Your client-side code reads only one chunk of the data from the server so that the server-side buffer fills up every second (although not by 100MB?). Does the problem still occur if you replace the client-side code with

const res = await fetch('/data');
const reader = res.body.getReader();
while (!await reader.read().then(function({value, done}) {
  console.log(value);
  return done;
}));

?

CodePudding user response:

I have tried again on different machines and I can conclude that the reason for this is Google Cloud Shell's bugs. It has nothing to do with Docker or ExpressJs.

  • Related