如何处理节点js中core-https中的代理错误
how to handle proxy error in core-https in node js
所以我正在使用 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();
所以我正在使用 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();