简体   繁体   中英

Downloading file in typescript in Node.js

I am trying to download a file with typescript (Node.js) using node-fetch.

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:

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:

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

Now the following code:

 fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});

returns an object with type WriteStream (checked here ) which implements stream.Writable. So I don't understands why doesn't the WriteStream object have the mentioned functions.

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? 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 .

To fix the error add import nodeFetch from "node-fetch"; and replace fetch(...) with nodeFetch(...) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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