我正在尝试在节点中发出请求。这是我的代码:
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
请求,这没有问题。显然,它与节点不同。
处理此问题的最佳方法是什么?