Я пытаюсь передать файл в ответ API, используя nest js и fs. Это просто и предположительно будет работать из коробки, но я получаю сообщение об ошибке:
_stream_readable. js: 638 dest.on ('unpipe', onunpipe); Ошибка типа: dest.on не является функцией
import { Controller, HttpCode, HttpStatus, Post, Res } from '@nestjs/common';
import { ApiBearerAuth, ApiInternalServerErrorResponse, ApiOkResponse, ApiTags,
ApiUnauthorizedResponse } from '@nestjs/swagger';
import { Response } from 'express';
import * as http from 'http';
import { SitemapExportRepository } from './sitemap-export.repository';
const fs = require('fs');
@Controller('api/v1/site-map')
@ApiTags('❤ API > Contents site-map')
export class SitemapExportController {
constructor(private readonly repo: SitemapExportRepository) {}
@Post()
@ApiBearerAuth()
@HttpCode(HttpStatus.OK)
@ApiOkResponse({description: http.STATUS_CODES[200]})
@ApiUnauthorizedResponse({description: http.STATUS_CODES[401]})
@ApiInternalServerErrorResponse({description: http.STATUS_CODES[500]})
async loadContentSupportPublications(@Res() response: Response): Promise<void> {
const readStream = fs.createReadStream('./dist/test');
readStream.on('open', () => {
readStream.pipe(response);
});
readStream.on('error', (err: Error) => {
console.log(err);
});
}
}
полная трассировка стека
_stream_readable.js:638
dest.on('unpipe', onunpipe);
^
TypeError: dest.on is not a function
at ReadStream.Readable.pipe (_stream_readable.js:638:8)
at ReadStream.readStream.on (/Users/medi/Documents/wkdir/mygit/site-map-api/dist/server.js:9017:32)
at ReadStream.emit (events.js:189:13)
at lazyFs.open (internal/fs/streams.js:120:10)
at FSReqWrap.args [as oncomplete] (fs.js:140:20)
/Users/medi/Documents/wkdir/mygit/site-map-api/node_modules/webpack-shell-plugin/lib/index.js:168
throw error;
^