nodejs request ECONNREFUSED - node.js

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);

Related

One signal create notification REST API fails sometimes

I am using oneSignal for push notifications node.js. I am using the create notification api to send notification to the users, but i dont know why it works some times and sometimes gives timeout error
sendNotificationToUser(data) {
try {
var notificationData = {}
notificationData.app_id = oneSignalAppId
notificationData.headings = {
en: "Heading"
}
notificationData.contents = {
en: data.message
}
notificationData.include_player_ids = [data.deviceId]
var headers = {
"Content-Type": "application/json; charset=utf-8"
}
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
}
var https = require("https")
var req = https.request(options, function (res) {
res.on("data", function (data1) {
console.log("Response:")
console.log(JSON.parse(data1))
})
})
req.on("error", function (e) {
console.log("ERROR:")
console.log(e)
})
req.write(JSON.stringify(notificationData))
req.end()
} catch (err) {
console.log("err in notification", err)
}
}
this api works 50% of times and 50% of times it responds with time out error, even all the inputs are correct
ERROR:
{
Error: connect ETIMEDOUT 104.18.225.52:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
errno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
address: '104.18.225.52',
port: 443
}
one simple solution is to directly hit the working ip, you can do this by including
host: 'onesignal.con' in headers and
host: '104.18.226.52' in options
this resolved my issue
to know more about how you can specify ip with host in https request
go here HTTPS request, specifying hostname and specific IP address

getting Error: getaddrinfo ENOTFOUND while performing rest api call in node.js using http.request

i have created api in node.js which consume set of api hosted at http://dev.abc.co.in:20081
not every time but randomly sometimes it throws the error
Error: getaddrinfo ENOTFOUND dev.abc.co.in
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'dev.abc.co.in'
}
to call those api i have used request node module because i started getting this error i switched to fetch-node npm module and finally replace the code with internal node module http but getting same error
here is the code i have written using http.request
try{
const options = {
hostname: "dev.abc.co.in",
port : 20081,
path: "/api/entity/workorder",
method: Config.method
};
if(Config.headers){
options.headers = Config.headers
}
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
callback(res, data);
});
req.socket.destroy();
}).on("error", (err) => {
console.log("===Error: ", err);
callback(null, err);
});
if(Config.method!="GET" && Config.body){
Config.headers["Content-Length"] = Config.body.length;
req.write(Config.body);
}
req.end();
}catch(e){
console.log("Exception=====",e);
}
as shown in error message issue related to DNS so i try to resolve this DNS using
node -pe 'require("dns").lookup("dev-vsg.dovertech.co.in",function(){console.dir(arguments)})
but still not resolved.
1) Omit 'http://' from the beginning of your demain and all slashes from the end or any path after the actual domain.
2) Try to resolve your hostname:
const dns = require('dns');
dns.resolve("testdomain.com", 'ANY', (err, records) => {
if (err) {
console.log("Error: ", err);
} else {
console.log(records);
}
});
If dns records has been returned, then you will know it's a node js problem and after that we can investigate further. If not, then it's a domain configuration issue.

NodeJS Get request not working with Proxy (ECONREFUSED)

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 !

Node.js SSL / HTTPS request to api.bitbucket.org gets weird ssl error

I am getting this weird error:
Making request to bitbucket api at path: /2.0/repositories/interos/eco-system-globe/commit/116c82c81b8d3e1b8c2fd3f352510fd09e66a02e
***Request stream error***: Error: write EPROTO 139717438125888:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:
at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:83:16) {
errno: 'EPROTO',
code: 'EPROTO',
syscall: 'write'
}
the code I have is simply:
const pth = `/2.0/repositories/${fullRepoName}/commit/${sha}`;
console.log('Making request to bitbucket api at path:',pth);
const newReq = https.get({ // formerly get
protocol: 'https:',
port: 80,
hostname: 'api.bitbucket.org',
path: pth,
headers: {
'Authorization': `Basic ${bitbucketBase64}`,
'Auth': `Basic ${bitbucketBase64}`
}
}, r => {
const v = {
data: ''
};
r.on('data', d => {
v.data += String(d || '');
});
r.once('end', () => {
console.log('response ended:', r.statusCode);
cb(null, v);
});
});
newReq.once('error', e => {
console.error('***Request stream error***:', e);
});
// newReq.write(stringified);
newReq.end();
anyone have an idea what that's about? I am on Node.js version 12.2.0
I had port: 80 but it should be port: 443 ... for SSL that's the default, so you can just omit the port argument.

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 }

Resources