[英]Downloading file in typescript in Node.js
I am trying to download a file with typescript (Node.js) using node-fetch.我正在尝试使用 node-fetch 下载带有 typescript (Node.js) 的文件。
As per the documentation and a stack overflow answer here the following code should work:根据此处的文档和堆栈溢出答案,以下代码应该可以工作:
public async downloadXMLFeed(): Promise<void>{
// function for download the file to
// a temporary location
let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
fetch(FILE_URL)
.then((response) => {
response.body.pipe(fileStream)
fileStream.on("finish", () => {
fileStream.close();
})
});
but I get the following error:但我收到以下错误:
Property 'pipe' does not exist on type 'Response'
I also checked the type file for response in node-fetch types.我还检查了节点获取类型中的响应类型文件。 It does not have a pipe function.它没有管道功能。
I checked the type definition for node-fetch here and it seems the body in response is a readable stream as per the interface here:我在这里检查了 node-fetch 的类型定义,根据这里的接口,响应体似乎是一个可读流:
export class Body {
constructor(body?: any, opts?: { size?: number; timeout?: number });
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Buffer>;
body: NodeJS.ReadableStream; // This should work.
bodyUsed: boolean;
buffer(): Promise<Buffer>;
json(): Promise<any>;
size: number;
text(): Promise<string>;
textConverted(): Promise<string>;
timeout: number;
} }
and has a function pipeTo (I found this from the documentationhere . I tried run the following code after going through the mentioned documentation:并且有一个函数 pipeTo (我从这里的文档中找到了这个。我在浏览了提到的文档后尝试运行以下代码:
public async downloadXMLFeed(): Promise<void>{
// function for download the file to
// a temporary location
let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
fetch(FILE_URL)
.then((response) => {
response.body.pipe(fileStream)
fileStream.on("finish", () => {
fileStream.close();
})
});
However I get the error:但是我收到错误:
Argument of type 'WriteStream' is not assignable to parameter of type 'WritableStream<Uint8Array>'.
Type 'WriteStream' is missing the following properties from type 'WritableStream': locked, abort, getWriter “WriteStream”类型缺少“WritableStream”类型中的以下属性:locked、abort、getWriter
Now the following code:现在下面的代码:
fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
returns an object with type WriteStream (checked here ) which implements stream.Writable.返回一个实现了 stream.Writable 的 WriteStream 类型的对象( 在这里检查)。 So I don't understands why doesn't the WriteStream object have the mentioned functions.所以我不明白为什么 WriteStream 对象没有提到的功能。
Also, how do I solve this?另外,我该如何解决这个问题? Is there a standard way of downloading http files in typescript with a node.js backend that I am not being able to figure out?是否有一种标准方法可以使用我无法弄清楚的 node.js 后端在打字稿中下载 http 文件? Or am I missing something here.或者我在这里遗漏了什么。
but I get the following error:但我收到以下错误:
Property 'pipe' does not exist on type 'Response'
This error is also triggered by this code这个错误也是由这段代码触发的
async function download() {
const res = await fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png');
await new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream('./octocat.png');
res.body.pipe(fileStream);
res!.body!.on("error", (err) => {
reject(err);
});
fileStream.on("finish", function() {
resolve();
});
});
}
taken from here .取自这里。 The error is triggered because the fetch
function is interpreted as JS fetch .触发错误是因为fetch
函数被解释为 JS fetch 。
To fix the error add import nodeFetch from "node-fetch";
修复错误添加import nodeFetch from "node-fetch";
and replace fetch(...)
with nodeFetch(...)
.并将fetch(...)
替换为nodeFetch(...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.