带有https代理的NodeJS EventSource不起作用

时间:2019-08-15 09:27:19

标签: node.js https proxy

我们有一个连接到服务器的客户端。可以同时访问服务器:http和https。 Https服务器包含CA认证的SSL密钥。 由于客户端在公司代理后面运行,因此我们使用以下命令:

localhost/magento/index.php

因此,当前命令仅在连接到http://server时有效。
如果我们尝试连接到https://server,则会收到以下错误:

const events = this.proxy ? new EventSource(this.source, { proxy: this.proxy }) : new EventSource(this.source)

因此我们尝试设置:

Event { type: 'error', status: 400, message: 'Bad Request' }

const events = this.proxy ? new EventSource(this.source, {https: {proxy: this.proxy, rejectUnauthorized: false} } ) : new EventSource(this.source)

在两种情况下,我们都发生了超时错误。

我们缺少什么吗?我们应该如何在代理后面设置https连接?

1 个答案:

答案 0 :(得分:1)

解决方案是使用'global-agent'或'global-tunnel-ng'软件包:

// Support working behind a corporate proxy
    const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10);

    if (MAJOR_NODEJS_VERSION >= 10) {
  // `global-agent` works with Node.js v10 and above. Proxy env should be defined "export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080"
      require('global-agent').bootstrap()
    } else {
  // `global-tunnel-ng` works only with Node.js v10 and below. Uses npm proxy settings
      require('global-tunnel-ng').initialize()
    }   

    const events = new EventSource(this.source)

在此处阅读更多信息:https://www.npmjs.com/package/global-agent

相关问题