如何在nodejs中从远程url创建可读流?

Mar*_*ick 25 video-streaming node.js nodejs-stream nodejs-server

在nodejs文档中,streams部分说我可以做fs.createReadStream(url || path)。但是,当我真正这样做时,它告诉我Error: ENOENT: no such file or directory。我只想将视频从可读流传输到可写流,但我坚持创建一个可读流。

我的代码

const express = require('express')
const fs = require('fs')
const url = 'https://www.example.com/path/to/mp4Video.mp4'
const port = 3000

app.get('/video', (req, res) => {
    const readable = fs.createReadStream(url)
})
app.listen(port, () => {
    console.log('listening on port ' + port)
})
Run Code Online (Sandbox Code Playgroud)

错误:

listening on port 3000
events.js:291
      throw er; // Unhandled 'error' event
      ^

Error: ENOENT: no such file or directory, open 'https://www.example.com/path/to/mp4Video.mp4'
Emitted 'error' event on ReadStream instance at:
    at internal/fs/streams.js:136:12
    at FSReqCallback.oncomplete (fs.js:156:23) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'https://www.example.com/path/to/mp4Video.mp4'
}
Run Code Online (Sandbox Code Playgroud)

PS: https://www.example.com/path/to/mp4Video.mp4 IS NOT THE ACTUAL URL

jfr*_*d00 34

fs.createReadStream()不适用于 http URL,仅适用于file://URL 或文件名路径。不幸的是,fs文档中没有对此进行描述,但是如果您查看源代码fs.createReadStream()遵循它所调用的内容,您会发现它最终调用了fileURULtoPath(url)如果不是file:URL 则会抛出异常。

function fileURLToPath(path) {
  if (typeof path === 'string')
    path = new URL(path);
  else if (!isURLInstance(path))
    throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
  if (path.protocol !== 'file:')
    throw new ERR_INVALID_URL_SCHEME('file');
  return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
}
Run Code Online (Sandbox Code Playgroud)

建议使用该got()库从 URL 获取读取流:

const got = require('got');
const mp4Url = 'https://www.example.com/path/to/mp4Video.mp4';

app.get('/video', (req, res) => {
    got.stream(mp4Url).pipe(res);
});
Run Code Online (Sandbox Code Playgroud)

本文描述了更多示例:如何使用 Got 在 Nodejs 中流式传输文件下载


您还可以使用普通http/https模块来获取读取流,但我发现got()在较高级别上对于许多 http 请求事物通常很有用,所以这就是我使用的。但是,这是带有 https 模块的代码。

const https = require('https');
const mp4Url = 'https://www.example.com/path/to/mp4Video.mp4';

app.get("/", (req, res) => {
    https.get(mp4Url, (stream) => {
        stream.pipe(res);
    });
});
Run Code Online (Sandbox Code Playgroud)

可以为这两种情况添加更高级的错误处理。

  • require('https') 版本运行得很好,无需安装另一个第 3 方包。 (2认同)