javascript - 如何处理节点js中core-https中的代理错误
问题描述
所以我使用 core-https 向 webstie 发出 Get 请求,我这样做是通过使用以下代码使用代理获取请求:
const { HttpsProxyAgent } = require("https-proxy-agent");
const proxy = new HttpsProxyAgent(`http://user:pass@host:port`);
https.get("https://www.google.com/",
{ agent: proxy },
(res) => {
var body = "";
res.on("data", function (chunk) {
body += chunk;
// console.log(body)
});
res.on("end", function () {
}
);
})
所以有时代理会无效或过期,甚至使用本地主机使用 fiddler 或 Charles 进行调试
const { HttpsProxyAgent } = require("https-proxy-agent");
const proxy = new HttpsProxyAgent(`http://127.0.0.1:8888`); // For Debugging
https.get("https://www.google.com/",
{ agent: proxy },
(res) => {
var body = "";
res.on("data", function (chunk) {
body += chunk;
// console.log(body)
});
res.on("end", function () {
}
);
})
如果我忘记打开代理调试器也会导致错误。
我试着这样做:
res.on("error" , function(e){
console.log("an error have been occurred ")
})
但似乎没有任何效果
解决方案
所以我找到了答案,它会像这样完成
https.get(
"https://www.google.com/",
{ agent: proxy },
(res) => {
var body = "";
res.on("data", function (chunk) {
body += chunk;
});
res.on("end", function () {
// console.log(body)
})
.on('error', function (e) {
console.error("error");
}).end();
推荐阅读
- json - 我有一个包含 379000 行的 JSON 文件,它在字符串对象中充满了双引号,我怎样才能删除所有这些?
- html - 如何删除我的部分顶部的额外间距
- python-3.x - 如何使用 svm 对数组进行分类?
- java - Akka:观看演员的网络开销
- selenium - 编写机器人框架测试用例以使用以下 Web 元素搜索搜索输入关键字?
- php - 如何将 PSR 日志文件更改为每天仅一个
- regex - 如何使用 Google 表格中的 IFS 和 AND 函数根据数字的小数位数创建条件?
- regex - RegEx 以匹配具有特定模式的电话号码
- c++ - 我的吸气剂给了我一个非标准的并且无法转换错误
- php - 只有 3 个按钮中的第一个按钮正在工作 - 使用 onclick 提交 - 有什么想法有什么问题吗?