javascript - 由于代理问题,Node.js 脚本失败

标签 javascript node.js proxy protractor

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

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

失败的脚本是here

它包含以下内容

#!/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 模块没有代理服务器的概念。

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

关于javascript - 由于代理问题,Node.js 脚本失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520238/

相关文章:

javascript - 具有相同大小的两个 div 的 Div(内部 img)

JavaScript 回调返回值

从 1.7.9 版本开始,git 不能在代理后面工作

linux - svn over HTTP 代理

apache - 从 apache 服务中排除 404

javascript - float 饼图工具提示百分比

javascript - 使用mutationobservers检测获取请求结果的变化

javascript - 使用 NightmareJS 时如何进行多个查询?

javascript - 未找到使用外部函数执行用户脚本的 NodeJS 模块

javascript - 在 JavaScript 类 : this vs. var 中声明变量。区别?