2

I find it hard to use superagent-proxy, just with the simple code:

const superagent = require('superagent')
require('superagent-proxy')(superagent)

let proxy = 'http://221.237.122.22:8118' // 设置代理

superagent
  .get('http://sf.gg')
  .proxy(proxy)
  .timeout(3600*1000)
  .end((err, res) => {
    console.log(res)
    console.log(res.status, res.headers);
    console.log(res.body);
  })

but when run, it cannot get an reply, why?

hanzichi
  • 609
  • 2
  • 12
  • 22

2 Answers2

1

you should:

const superagent = require('superagent')
require('superagent-proxy')(superagent)

let proxy = 'http://221.237.122.22:8118' // 设置代理

superagent
  .get('http://sf.gg')
  .proxy(proxy)
  .timeout(3600*1000)
  .end((err, res) => {
    if(err) {
       console.error(err);
       return;
    }
    console.log(res)
    console.log(res.status, res.headers);
    console.log(res.body);
  })

then, you will get error such as

{ Error: connect ECONNREFUSED 221.237.122.22:8118
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '221.237.122.22',
  port: 8118,
  response: undefined }
kwoktung
  • 572
  • 1
  • 4
  • 12
0

You code is correct, proxy URL is not - if its exactly 'http://221.237.122.22:8118' it means proxy do not require any login, anybody can use it with just URL, its not the case for most of the proxies, normally proxy URL is like 'http://username:password@IPADDRESS_OR_HOST:PORT'