函数"/>
Nestjs流文件dest.on不是函数
我尝试使用nestjs和fs将文件流式传输到api响应上。这件事很简单,应该可以直接使用,但是我遇到了错误:
_ stream_visible.js:638dest.on('unpipe',onunpipe);TypeError: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;
^
回答如下:[通过使用服务写入内存缓冲区并通过http响应流结果找到了解决方案。
enter code here
@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 buffer = readable({objectMode: true});
buffer._read = () => {};
response.type('application/json').send(buffer);
await this.sitemapExportRepository.streamTreeSiteMap(buffer);
}
更多推荐
Nestjs流文件dest.on不是函数
发布评论