首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理节点js中的核心-https中的代理错误

如何处理节点js中的核心-https中的代理错误
EN

Stack Overflow用户
提问于 2021-06-03 12:45:02
回答 1查看 123关注 0票数 3

因此,在使用core-https对webstie执行Get请求时,通过使用以下代码,我正在使用代理进行此获取请求:

代码语言:javascript
复制
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进行调试。

代码语言:javascript
复制
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 () {
    }
  );
  
})

如果我忘记打开代理调试器,也会导致错误。

我试过这样做:

代码语言:javascript
复制
  res.on("error" , function(e){
            console.log("an error have been occurred  ")
          })

但似乎什么都起不到作用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-03 14:33:29

所以我找到了答案,就像这样

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67821726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档