流式传输 MP4 而不是使用 Express 下载



我有一个可以下载 mp4 文件的 URI,我正在尝试创建一个代理来流式传输该文件而不是下载它,但我尝试过的所有内容仍然下载文件而不是流式传输它。内容类型设置为视频/mp4,这是我能够找到的唯一答案

res.contentType('video/mp4');
req.pipe(request.get(url.fullUrl)).pipe(res);

我认为您必须创建一个可读的流,然后将其管道传输到响应,如下所示:

const path = 'video.mp4';
const head = {
'Content-Length': 123456, // Get the video file size,
'Content-Type': 'video/mp4',
}
res.writeHead(200, head);
fs.createReadStream(path).pipe(res);

最新更新