6

node 原生模块下载失败问题

 1 year ago
source link: https://wangyulue.com/2022/08/node-%E5%8E%9F%E7%94%9F%E6%A8%A1%E5%9D%97%E4%B8%8B%E8%BD%BD%E5%A4%B1%E8%B4%A5%E9%97%AE%E9%A2%98/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用 http 模块下载 https 的链接会报异常

这时候需要使用 https 模块下载,下面是兼容 http 和 https 的下载函数:

const fs = require("fs");
const path = require("path");
const http = require("http");
const https = require("https");

async function download(url, filePath) {
  const proto = url.startsWith("https") ? https : http;

  return new Promise((resolve, reject) => {
    const file = fs.createWriteStream(filePath);
    let fileInfo = null;

    // const request = proto.get(url, response => {
    const request = proto.get(url, (response) => {
      if (response.statusCode !== 200) {
        fs.unlink(filePath, () => {
          reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
        });
        return;
      }

      fileInfo = {
        mime: response.headers["content-type"],
        size: parseInt(response.headers["content-length"], 10),
      };

      response.pipe(file);
    });

    // The destination stream is ended by the time it's called
    file.on("finish", () => resolve(fileInfo));

    request.on("error", (err) => {
      fs.unlink(filePath, () => reject(err));
    });

    file.on("error", (err) => {
      fs.unlink(filePath, () => reject(err));
    });

    request.end();
  });
}

const url = "https://100.100.22.140/files/1.0.zip";

download(url, path.resolve("./download"));

目标下载地址是自签的 https 证书,下载会报异常

但是如果目标下载地址是自签的 https 证书,则下载也会失败,这时候有两种解决方式:

第一种:环境变量设置 NODE_TLS_REJECT_UNAUTHORIZED

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

第二种:https 模块下载时加上 { rejectUnauthorized: false } 可选项

具体的例子如下:

const fs = require("fs");
const path = require("path");
const http = require("http");
const https = require("https");

// process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

async function download(url, filePath) {
  const proto = url.startsWith("https") ? https : http;

  return new Promise((resolve, reject) => {
    const file = fs.createWriteStream(filePath);
    let fileInfo = null;

    // const request = proto.get(url, response => {
    const request = proto.get(
      url,
      { rejectUnauthorized: false },
      (response) => {
        if (response.statusCode !== 200) {
          fs.unlink(filePath, () => {
            reject(
              new Error(`Failed to get '${url}' (${response.statusCode})`)
            );
          });
          return;
        }

        fileInfo = {
          mime: response.headers["content-type"],
          size: parseInt(response.headers["content-length"], 10),
        };

        response.pipe(file);
      }
    );

    // The destination stream is ended by the time it's called
    file.on("finish", () => resolve(fileInfo));

    request.on("error", (err) => {
      fs.unlink(filePath, () => reject(err));
    });

    file.on("error", (err) => {
      fs.unlink(filePath, () => reject(err));
    });

    request.end();
  });
}

const url = "https://100.100.22.140/files/1.0.zip";

download(url, path.resolve("./download"));

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK