节点.js脚本由于代理问题而失败

Node.js script fails due to proxy problems

本文关键字:代理 问题 失败 js 脚本 于代理 节点      更新时间:2023-09-26

我正在尝试在Mac中安装量角器。安装是通过命令行完成的,但由于连接问题,其中一个脚本失败。主要问题是我在公司代理服务器后面。

我在控制台中设置了代理服务器,npm 也配置了正确的代理设置。

失败的脚本在这里

它包含以下内容

#!/usr/bin/env node
var fs = require('fs');
var os = require('os');
var url = require('url');
var http = require('http');
var AdmZip = require('adm-zip')
// Download the Selenium Standalone jar and the ChromeDriver binary to
// ./selenium/
// Thanks to http://www.hacksparrow.com/using-node-js-to-download-files.html
// for the outline of this code.
var SELENIUM_URL =
    'http://selenium.googlecode.com/files/selenium-server-standalone-2.35.0.jar';
var CHROMEDRIVER_URL_MAC =
    'https://chromedriver.googlecode.com/files/chromedriver_mac32_2.2.zip';
var CHROMEDRIVER_URL_LINUX32 =
    'https://chromedriver.googlecode.com/files/chromedriver_linux32_2.2.zip';
var CHROMEDRIVER_URL_LINUX64 =
    'https://chromedriver.googlecode.com/files/chromedriver_linux64_2.2.zip';
var CHROMEDRIVER_URL_WINDOWS =
    'https://chromedriver.googlecode.com/files/chromedriver_win32_2.2.zip';
var DOWNLOAD_DIR = './selenium/';
var START_SCRIPT_FILENAME = DOWNLOAD_DIR + 'start';
var chromedriver_url = '';
var start_script = 'java -jar selenium/selenium-server-standalone-2.35.0.jar';

if (!fs.existsSync(DOWNLOAD_DIR) || !fs.statSync(DOWNLOAD_DIR).isDirectory()) {
  fs.mkdirSync(DOWNLOAD_DIR);
}
console.log(
  'When finished, start the Selenium Standalone Server with ./selenium/start 'n');
// Function to download file using HTTP.get
var download_file_httpget = function(file_url, callback) {
  console.log('downloading ' + file_url + '...');
  var options = {
    host: url.parse(file_url).host,
    port: 80,
    path: url.parse(file_url).pathname
  };
  var file_name = url.parse(file_url).pathname.split('/').pop();
  var file_path = DOWNLOAD_DIR + file_name;
  var file = fs.createWriteStream(file_path);
  http.get(options, function(res) {
    res.on('data', function(data) {
      file.write(data);
    }).on('end', function() {
      file.end(function() {
        console.log(file_name + ' downloaded to ' + file_path);
        if (callback) {
          callback(file_path);
        }
      });
    });
  });
};
download_file_httpget(SELENIUM_URL);
if (!(process.argv[2] == '--nocd')) {
  if (os.type() == 'Darwin') {
    chromedriver_url = CHROMEDRIVER_URL_MAC;
  } else if (os.type() == 'Linux') {
    if (os.arch() == 'x64') {
      chromedriver_url = CHROMEDRIVER_URL_LINUX64;
    } else {
      chromedriver_url = CHROMEDRIVER_URL_LINUX32;
    }
  } else if (os.type() == 'Windows_NT') {
    chromedriver_url = CHROMEDRIVER_URL_WINDOWS;
  }
  var chromedriver_zip = chromedriver_url.split('/').pop();
  start_script += ' -Dwebdriver.chrome.driver=./selenium/chromedriver';
  download_file_httpget(chromedriver_url, function(file_name) {
    var zip = new AdmZip(file_name);
    zip.extractAllTo(DOWNLOAD_DIR);
    if (os.type() != 'Windows_NT') {
      fs.chmod(DOWNLOAD_DIR + 'chromedriver', 0755);
    }
  });
}
var start_script_file = fs.createWriteStream(START_SCRIPT_FILENAME);
start_script_file.write(start_script);
start_script_file.end(function() {
  fs.chmod(START_SCRIPT_FILENAME, 0755);
});

我们如何修改此脚本以解决连接问题?

download_file_httpget中,你称http.get(...) 。 这将直接连接到目标服务器。

在 npm 中配置代理只会影响 npm。 Node的http模块没有代理服务器的概念。

如果需要通过代理发出请求,请考虑使用支持代理的请求模块。