如何从nodejs中的远程url创建可读流? [英] how to create a readable stream from a remote url in nodejs?

查看:30
本文介绍了如何从nodejs中的远程url创建可读流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

on nodejs documentation, the streams section says I can do fs.createReadStream(url || path). But, when I actually do that It tells me 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.

我的代码:

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

错误:

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 不是实际网址

推荐答案

fs.createReadStream() 不适用于 http URLs only file:// URLs or filename路径.不幸的是,这在 fs 文档中没有描述,但是如果您查看 源代码 fs.createReadStream() 并按照它的调用,你会发现它最终调用了 fileURULtoPath(url) 如果它不是 file: URL 就会抛出.

fs.createReadStream() does not work with http URLs only file:// URLs or filename paths. 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.

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

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

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

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

More examples described in this article: How to stream file downloads in Nodejs with Got.

您也可以使用普通的 http/https 模块来获取读取流,但我发现 got() 在更高级别上通常对许多 http 有用请求东西,这就是我使用的东西.但是,这是带有 https 模块的代码.

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. But, here's code with the https module.

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.

这篇关于如何从nodejs中的远程url创建可读流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆