NodeJS Get request not working with Proxy (ECONREFUSED) - node.js

I'm trying to use NodeJS get requets using the 'request' module, so here is my code:
var request = require('request');
request({
'url':'http://whatismyip.host/',
'method': "GET",
'proxy': 'http://181.112.225.78:35482'
},function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
else{
console.log(error);
}
})
When I remove the "'proxy': 'http://181.112.225.78:35482'" line, it works perfectly, but when I let it, I have this error:
{ Error: connect ECONNREFUSED 181.112.225.78:35482
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '181.112.225.78',
port: 35482 }
I already tried to change the proxy, and I got the same error, so I have no idea where is it coming from...
Thanks you !

Related

node js won't let me use proxies

I'm trying to use the request-module to scrape websites and get the response-HTML. To prevent getting blocked by the target sites I need to use some proxies. But for some reason I can not accomplish to use request with a proxy.
I've also tried many different proxies and none of them will do the job.
My code:
const request = require('request');
const cheerio = require('cheerio');
var servers = [
'http://173.249.48.240:8080',
'http://85.214.250.48:3128'
]
var options = {
'url': 'http://www.google.de',
'proxy': servers[1]
};
request(options, (err,res,body) => {
console.log(res);
console.log(body);
console.log(err);
try {
var $ = cheerio.load(body);
console.log($('#ipv4').html());
} catch(err) {
//do nothing
}
})
I've tried many different proxies and have also used nightmare before but everytime I try to connect with a Proxy I get the following exception:
{ Error: connect ECONNREFUSED 85.214.250.48:3128
at Object._errnoException (util.js:1024:11)
at _exceptionWithHostPort (util.js:1046:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '85.214.250.48',
port: 3128 }

how to use superagent-proxy?

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?
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 }
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'

nodejs request ECONNREFUSED

i have some ECONNREFUSED with 'request' module, but some time my request passe without error O_o ...
So i make my request recurcive but this not solve the problème ...
let request = require("request");
let currency = 'btceur';
let data = [];
let url = "https://api.cryptowat.ch/markets/kraken/" + currency + "/price";
let nbTry = 0;
let nbMaxTry = 5;
let callbackRequest = (error, response, body) => {
if (error || response.statusCode != 200) {
console.log('error', 'error, retry ' + (nbTry + 1) + "/" + nbMaxTry);
console.log(error);
if (nbTry <= nbMaxTry) {
nbTry++;
request(url, callbackRequest);
} else {
console.log(data);
}
} else {
let bodyjson = JSON.parse(body);
bodyjson.result.currency = currency;
data.push(bodyjson.result);
console.log(data);
}
};
request(url, callbackRequest);
console output:
error error, retry 1/5
{ Error: connect ECONNREFUSED 69.164.196.116:443
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '69.164.196.116',
port: 443 }
[ { price: 1113.5, currency: 'btceur' } ]
api.cryptowat.ch resolves to two IP-numbers, 23.239.28.55 and 69.164.196.116. The latter is giving issues (for me as well).
You could try using the former for each request as a temporary workaround:
let url = "https://23.239.28.55/markets/kraken/" + currency + "/price";
It doesn't seem to need a Host header, although it would probably be better if you passed one anyway:
request({ url, headers : { Host : 'api.cryptowat.ch' } }, callbackRequest);

nodeJS request POST To localhost:3000 ECONNREFUSED

At one point I just want to make a POST request to my rails server running on localhost:3000
If a use like postman on even cUrl I can ping my API
But kow, with my node app I want to do this :
var req_body = {
'my_id': id,
'dataTable': data }
var options = {
method: 'POST',
url: config.get('api_url') + 'end_tracking',
json: true,
body: req_body
}
// config.get('api_url') return http://localhost:3000/
request(options, function (error, response, body) {
console.log(error)
... }
this is what I get :
{ Error: connect ECONNREFUSED 127.0.0.1:3000
at Object.exports._errnoException (util.js:896:11)
at exports._exceptionWithHostPort (util.js:919:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1073:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3000 }
and as I said, if I do this request directly with postman, everything works !
I finally did it !
For a unknown reason, accessing localhost:3000, launched with rails s didn't work at all on my node server
but I worked when I started the rails server with rails s -b 0.0.0.0 !
how i did this , just use the same structure and i hope it works for you
request({
url: url, //URL to hit
method: 'post',
headers: headers,
timeout: 10000,
body: JSON.stringify(body)
}, function (error, result, body) {
if (error) {
console.log(error);
} else if (result.statusCode == 500) {
console.log('not found');
} else {
console.log('success')
}
});
hope it works.

Error: getaddrinfo ENOTFOUND alerts.sinfini.com

i'm trying to call the url for sending SMS, getting this kind of error
{ [Error: getaddrinfo ENOTFOUND alerts.sinfini.com]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'alerts.sinfini.com' }
my node js code is
var request = require('request');
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}else{
console.log(error);
}
});
In the above code where is the URL defined? Have you checked the URL working fine via rest clients! ENoT thrown for either URL or DNS is not get resolved

Resources