rakibtg
rakibtg

Reputation: 5911

Streaming a large remote file

I need to serve local files from a different server using node. The api endpoint are being handled by express.

The goal is not to contain the entire file in memory instead stream the data so it shows the output to the enduser progressively.

By reading the stream api documentation i came up with this solution with a combination with expressjs response. Here is the example:

  const open = (req, res) => {
    const formattedUrl = new url.URL(
      "https://dl.bdebooks.com/Old%20Bangla%20Books/Harano%20Graher%20Jantra%20Manob%20-%20Shaktimoy%20Biswas.pdf"
    );
    const src = fs.createReadStream(formattedUrl);
    return src.pipe(res);
  };

But when i hit this express endpoint http://localhost:3000/open it throws following error:

TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file

I would like to display the file content inline! What I am doing wrong? Any suggestions will be greatly appreciated. :)

Upvotes: 0

Views: 1453

Answers (1)

jfriend00
jfriend00

Reputation: 707318

fs.createReadStream() operates on the file system. It does not accept an http or https URL. Instead, you need to use something like http.get() to make an http request and then return a readable stream that you can then pipe from.

const open = (req, res) => {
  const formattedUrl = new url.URL("https://dl.bdebooks.com/Old%20Bangla%20Books/Harano%20Graher%20Jantra%20Manob%20-%20Shaktimoy%20Biswas.pdf");
  http.get(formattedUrl, (stream) => {
      stream.pipe(res);
  }).on('error', (err) => {
      // send some sort of error response here
  });
};

Upvotes: 1

Related Questions