简体   繁体   English

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

[英]how to create a readable stream from a remote url in nodejs?

on nodejs documentation, the streams section says I can do fs.createReadStream(url || path) .在 nodejs 文档中,流部分说我可以做fs.createReadStream(url || path) But, when I actually do that It tells me Error: ENOENT: no such file or directory .但是,当我实际这样做时,它告诉我Error: ENOENT: no such file or directory I just want to pipe the video from a readable to a writable stream, But I'm stuck on creating a readable one.我只想 pipe 将视频从可读到可写 stream,但我坚持创建可读的视频。

my code :我的代码

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)
})

the ERROR:错误:

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'
}

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

fs.createReadStream() does not work with http URLs only file:// URLs or filename paths. fs.createReadStream()不适用于 http URL 仅file:// URL 或文件名路径。 Unfortunately, this is not described in the fs doc, but if you got look at the source code for fs.createReadStream() and follow what it calls you can find that it ends up calling fileURULtoPath(url) which will throw if it's not a 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);
}

It would suggest using the got() library to get yourself a readstream from a URL:建议使用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);
});

More examples described in this article: How to stream file downloads in Nodejs with Got .本文中描述的更多示例: 如何在 Nodejs 中使用 Got stream 文件下载


You can also use the plain http/https modules to get the readstream, but I find got() to be generally useful at a higher level for lots of http request things so that's what I use.您也可以使用普通的http/https模块来获取读取流,但我发现got()通常在更高级别上对许多 http 请求的东西有用,所以这就是我使用的。 But, here's code with the https module.但是,这里是 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);
    });
});

More advanced error handling could be added to both cases.这两种情况都可以添加更高级的错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM