我正在用雀巢公司建一个视频流网络服务器。
我遵循了nest docs中介绍的完全相同的方法。
但是,有一些错误...
我的文件.控制器.ts
import { Controller, Get, StreamableFile, Response } from '@nestjs/common';
import { createReadStream } from 'fs';
import { join } from 'path';
@Controller('file')
export class FileController {
@Get()
getOne(@Response({ passthrough: true }) res): StreamableFile {
const file = createReadStream(join(process.cwd(), 'sampleVideo.mp4'));
res.set({
'Content-Type': 'video/mp4',
});
return new StreamableFile(file);
}
}
错误代码
[Nest] 21224 - 2022. 08. 02. 4:41:16 ERROR [ExpressAdapter] Premature close
Error: Premature close
at new NodeError (node:internal/errors:372:5)
at ServerResponse.onclose (node:internal/streams/end-of-stream:142:30)
at ServerResponse.emit (node:events:539:35)
at Socket.onServerResponseClose (node:_http_server:236:23)
at Socket.emit (node:events:539:35)
at TCP.<anonymous> (node:net:709:12)
Error: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:372:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at ServerResponse.header (C:\Users\whan9\mvp-backend-nestjs\node_modules\express\lib\response.js:794:10)
at ServerResponse.send (C:\Users\whan9\mvp-backend-nestjs\node_modules\express\lib\response.js:174:12)
at StreamableFile.handleError (C:\Users\whan9\mvp-backend-nestjs\node_modules\@nestjs\common\file-stream\streamable-file.js:14:17)
at ReadStream.<anonymous> (C:\Users\whan9\mvp-backend-nestjs\node_modules\@nestjs\platform-express\adapters\express-adapter.js:43:22)
at Object.onceWrapper (node:events:642:26)
at ReadStream.emit (node:events:539:35)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
环境
Windows 10
node version: v16.16.0
npm: 8.15.1
sampleVideo.mp4
是可以的,它存在于正确的位置。
如果我摆脱res.set({'Content-Type': 'video/mp4'});
,视频流数据下载没有错误,而且任何文件扩展名。
我想知道我错过了什么...
1条答案
按热度按时间ycl3bljg1#
对于设置头错误。在Streaming Files上的NestJS文档中,它使用了**@Res装饰器而不是@Response**。这可能只是一个别名,但这是我在您的代码中找到的与文档和我的实现的唯一区别。
发生过早关闭错误是因为请求在开始发送流之前就被快速取消了。从August 16, 2022开始,NestJS现在可以捕获这个过早关闭错误,这样它就不会再导致服务器崩溃。