如何通过获取节点发出http请求?节点获取

时间:2019-12-06 00:43:39

标签: node.js fetch

我正在尝试在节点中发出请求。这是我的代码:

fetch("http://www.test.com", options).catch(err => {
    console.error(err);

    return err;
  });

这是我得到的错误。 TypeError: Only HTTP(S) protocols are supported

我试图创建一个新的代理并将其传递给这样的选项:

const httpAgent = new http.Agent({
    keepAlive: true
});
const httpsAgent = new https.Agent({
    keepAlive: true
});

const options = {
    agent: function (_parsedURL) {
        if (_parsedURL.protocol == 'http:') {
            return httpAgent;
        } else {
            return httpsAgent;
        }
    }
}

我仍然遇到相同的错误。

当我在客户端中运行此命令时,可以向同一个http发送一个https或一个fetch请求,这没有问题。显然,它与节点不同。

处理此问题的最佳方法是什么?

0 个答案:

没有答案