Home  >  Article  >  Web Front-end  >  How to download files in nodejs

How to download files in nodejs

王林
王林Original
2023-05-27 20:43:051363browse

In Node.js, you can use node-fetch and fs modules to download files. Here's how to use these two modules to download files.

First you need to install the node-fetch and fs modules. You can use the npm command to install:

npm install node-fetch fs

After the installation is complete, you can use these two modules.

Downloading files

The process of downloading files can be divided into several steps:

  1. Send HTTP request to obtain file data
  2. Write the data File

The following is a sample code for downloading a file:

const fetch = require('node-fetch');
const fs = require('fs');

const downloadFile = async (url, dest) => {
  const response = await fetch(url);

  if (!response.ok) {
    throw new Error(`failed to download file: ${response.statusText}`);
  }

  const fileStream = fs.createWriteStream(dest);
  await new Promise((resolve, reject) => {
    response.body.pipe(fileStream);
    response.body.on("error", (err) => {
      reject(err);
    });
    fileStream.on("finish", function() {
      resolve();
    });
  });
};

const fileUrl = 'https://example.com/files/file.txt';
const destPath = 'file.txt';

downloadFile(fileUrl, destPath)
  .then(() => {
    console.log('file downloaded successfully');
  })
  .catch((err) => {
    console.error('failed to download file:', err);
  });

This code first uses the node-fetch module to send an HTTP request to obtain file data, and then uses the fs module to write the data to the file . It should be noted that during the process of obtaining file data, you need to check whether the HTTP response status code is 200. If not, the download failed.

The code uses async/await asynchronous syntax and Promise objects to ensure that the file data is written before the result is output.

More options

The above example code is just a simple example of downloading a file, if you need more options and functions, you can adjust the code with other parameters.

  1. Specify HTTP request headers

You can use the headers parameter of node-fetch to specify HTTP request headers, for example:

const headers = {
  'Authorization': 'Bearer ' + token,
  'Content-Type': 'application/json'
};
const response = await fetch(url, {
  headers: headers
});
  1. Restrict downloads Speed

If you need to limit the download speed, you can use the third-party module speedline, for example:

const speedline = require('speedline');

const fileUrl = 'https://example.com/files/file.txt';
const destPath = 'file.txt';
const maxSpeed = 100; // 100 KB/s

const response = await fetch(fileUrl);
const stream = response.body;
const fileStream = fs.createWriteStream(destPath);
await speedline(stream.pipe(fileStream), maxSpeed);
  1. Cache download file

If you need to cache To download files, you can use the third-party module node-cache, for example:

const NodeCache = require('node-cache');
const fileCache = new NodeCache();

const downloadFile = async (url, dest) => {
  const cachedData = fileCache.get(url);
  if (cachedData) {
    return fs.writeFileSync(dest, cachedData);
  }
  
  const response = await fetch(url);

  if (!response.ok) {
    throw new Error(`failed to download file: ${response.statusText}`);
  }

  const fileData = await response.text();

  fileCache.set(url, fileData);
  fs.writeFileSync(dest, fileData);
};

The above is the basic knowledge and sample code for downloading files with Node.js. According to the actual needs and network environment, you can choose the parameters and methods that suit you to download files.

The above is the detailed content of How to download files in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn