Nestjs流文件dest.on不是函数

编程入门 行业动态 更新时间:2024-10-10 21:26:26

Nestjs流文件dest.on不是<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数"/>

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不是函数

本文发布于:2024-05-07 14:15:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   文件   Nestjs   dest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!