0

我正在尝试在我的 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);
});

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

4

1 回答 1

2

download_file_httpget,你在打电话http.get(...)。这将直接连接到目标服务器。

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

如果您需要通过代理发出请求,请考虑使用request 模块,它确实支持代理。

于 2013-10-22T16:14:34.840 回答