如何将文件作为可读流提供?
我正在尝试创建一个提供静态文件的请求
对象。 问题是:我需要做返回新响应(<可读流>)
。但是,Node的fs.CreateReadStream
返回readstream
(node api),而不是readableStream
(我相信这是Web API)。
从文件中的节点中生成readableStream
的正确方法是什么?
上下文:
- 我正在使用remix.run。在remix.run中,我需要返回一个普通
响应
对象。普通的响应
对象可以接受缓冲区或readableStream
。 - 这是
请求
对象: https:/ /developer.mozilla.org/en-us/docs/web/api/request
I'm trying to create a Request
object that serves a static file.
The issue is: I need to do return new Response(<readable stream>)
. However, Node's fs.createReadStream
returns a ReadStream
(Node API) instead of a ReadableStream
(which I believe is a web API).
What is the correct way to generate a ReadableStream
in Node from a file?
The context:
- I am using remix.run. In remix.run, I need to return a plain
Response
object. A plainResponse
object can either accept a buffer or aReadableStream
. - This is a
Request
object: https://developer.mozilla.org/en-US/docs/Web/API/Request
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何发现此问题的人 - 您可以使用
createAldableStreamFromReadable
将readstream
转换为可读
,可用于创建wendment> wendment :
For anyone finding this issue - you can use
createReadableStreamFromReadable
to convertReadStream
into aReadable
which can be used to create aResponse
:我不确定我是否有所有要素可以理解
返回新响应的原因(&lt;可读流&gt;)
,所以我假设您只需要在响应中发送文件。混音具有我们所谓的“ 资源路由除了渲染组件(这种类型的路线都不会导出)以外,作为更通用的端点。您将能够导出可以管理不同请求方法并使用文件和标头返回新响应的加载程序。
采取此示例来自上面链接的文档想要渲染一个允许下载pdf的组件:
您会注意到
&lt; link to = =“ pdf” reloAddocument&gt;
其中“ pdf”与下面的路由匹配:希望它对您有用,如果我猜正确了您需要什么!
I'm not sure I have all the elements to understand the reason for
return new Response(<readable stream>)
so I'm going to assume that you just need to send a file in a Response.Remix has what we call "Resource Routes", which are used for something other than rendering a component (this type of route does not export any), as a more general-purpose endpoint. You'll be able to export a loader that can manage different request methods and returns a new Response with a file and a header.
Take this example from the doc linked above which wants to render a component allowing to download a pdf:
You will notice
<Link to="pdf" reloadDocument>
where "pdf" matches the route below:Hoping that it will be useful to you if I guessed correctly what you needed!