'NestJS Streaming Files technique not working

I am trying to stream CSV file using NestJS Streaming Files technique, but the request seems to be stuck. Am I missing something here?

Sample code snippet:

import {StreamableFile} from '@nestjs/common';
import * as fs from 'fs';

@Get('csv')
csvStream(
  @Req() req: Request,
  @Res() res: Response,
): StreamableFile {
  const file = 'test.csv';
  const readStream = fs.createReadStream(file);
  readStream.on('data', (chunk) => console.log(chunk));  <--- the data log gets printed
  readStream.on('finish', () => console.log('done'));
  return new StreamableFile(readStream);
}

The data log gets printed (check statement - readStream.on('data', (chunk) => console.log(chunk))), and output is similar to below:

<Buffer 23 4a 45 67 11 97 ... 1022 more bytes>

and the request stays stuck in this state.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source