ECONNRESET http client node.js - node.js

I have a node.js app, which test uptime of websites.
I'm using HTTP client library 'request' (https://www.npmjs.com/package/request) to scan my differents websites but from one client of mine, I frequently get error :
ECONNRESET
All other domains (hundreds of them) are working fine. This client is using incapsula CDN.
The app is running in a docker EC2 cluster in AWS. I tried to switch today to another HTTP client library 'Axios' (https://www.npmjs.com/package/axios) but still the same error.
Any idea on how can I get a solution ?

You've to manage network error:
devices down
host unreachable
service on host unreachable
service on host restarted (TCP connexion broken)
Here is a part of a code to do this:
Connexion.on('error', (err) => {
console.log(" Disconnected. Velbus reusedSocket:",Connexion.reusedSocket, " err.code:", err.code)
if (Connexion.reusedSocket && err.code === 'ECONNRESET') {
// retriableRequest();
}
});
Late answer but if that could help others...

Related

Run a VPN client on a remote website

I have been researching this for day and I haven't been able to find the way to do this.
I am building a react app, running express at the backend, that needs to access some data in a remote database that lives inside a VPN. At the moment the app lives on my localhost so its enough for me to connect my machine using openvpn client and everything works a beauty. The problem will rise when the app will be live and I will need it to have access to the vpn by (I'm guessing) having a vpn client running on the site/domain.
Has anyone done this before?
I have tried to install the node-openvpn package that seems could do the job but unfortunately I can't manage to make it work as the connection doesn't seem to be configured properly.
This is the function I call to connect to the vpn that systematically fails at the line
--> openvpnmanager.authorize(auth);
const openvpnmanager = require('node-openvpn');
...
const connectToVpn = () => {
var opts = {
host: 'wopr.remotedbserver.com',
port: 1337, //port openvpn management console
timeout: 1500, //timeout for connection - optional,
logpath: '/log.txt'
};
var auth = {
user: 'userName',
pass: 'passWord',
};
var openvpn = openvpnmanager.connect(opts);
openvpn.on('connected', function() {
console.log('connecting..');
openvpnmanager.authorize(auth); <-- Error: Unhandled "error" event. (Cannot connect)
});
openvpn.on('console-output', function(output) {
console.log(output)
});
openvpn.on('state-change', function(state) { //emits console output of openvpn state as a array
console.log(output)
});
};
Am I misusing this function? Is there a better way?
Any help will be extremely appreciated.
Thank You!
The problem will rise when the app will be live and I will need it to
have access to the vpn by (I'm guessing) having a OpenVPN client running
on the site/domain.
Thats correct, you will need an openvpn client instance on the server where you will run the backend.
The above library (node-openvpn) is simply a library to interact with the local OpenVPN client instance. It cannot create a connection on its own. It depends on the OpenVPN binary (which should be running).
The solution you need is simply run the OpenVPN client on your server (apt-get openvpn). And let the daemon run. Check out the references below.
node-openvpn issues that points out that a running instance of the client is needed
OpenVPN CLI tutorial

Getting 502 Bad Gateway: Registered endpoint failed to handle the request when tried to connect mongodb using node js in PCF platform

I have mongodb in AWS EC2 instance.
I am trying to connec to mongodb instance from nodejs server using mongoClient. My node application is deployed on PCF platform.
When i try to connect to mongodb
it throws an error "502 Bad Gateway:
Registered endpoint failed to handle the request". My node application is working fine, one the connection with mongodb is causing the problem.
conn_str += req.params.dbname;
mongoClient.connect(conn_str, function(err, db) {
if(err) {
res.end(err);
} else {
res.end(db);
}
});
Help much appreciated.
Ok. I had faced a similar issue while I was trying to connect to my mongo cluster which was deployed on Google Compute Engine. The below checklist helped me resolve the issue
Check if your firewall and make sure that the port 27017 is open for external requests
Check if your MongoDB is up and running (just do a curl localhost:27017 from terminal) if it's running you should get the protocol error
Check if your MongoDB is configured to accept requests from applications that is outside it's network (if you're running on different VPC) -- By default, MongoDB doesn't allow external requests

Socket.io failing to connect because of nsp

I have a problem with socket.io#^1.0. The setup is fine because it works locally, the server is correctly configured and when i try to connect to the server from my Angular APP it works fine with this:
io.connect("localhost:8080");
The connection is established and i can send and receive event. Now in the production environment, "locahost:8080" is replaced with the address of the server Launched:
io.connect("https://domain-name.com/api");
I know that the problem here is the /api, since socket.io is considering it as a namespace and it's trying to connect to it, in my network console I see 500 Internal server error with the address https://domain-name.com without the /api when i replace the request url to add the /api I get a 200 OK with type octet-stream.
So the question here is: how do I connect to the correct path without consideration of the namespace?
Thanks in advance for any help :)
I think you want to use the path option (documented here):
// client
var socket = io.connect('https://domain-name.com/', {
path : '/api/socket.io'
});

node.js error ECONNREFUSED on localhost url only

I have a node app, hosted on heroku, where I'm trying to hit an API that returns some JSON. That API I'm calling lives with the same app that I'm calling it from (e.g. The node app hosts my web app and has an API that generates JSON from my db).
I'm using the request npm module to make my API call. When deployed to heroku, when I use localhost:8080 as the url in the function I get the error below, but when I use the actual url where my app is hosted on heroku, it works fine. I have dev/staging/prod environments so I don't want to hard code the url.
Thing is, it works fine locally on my machine, so I'm not sure what the issue is - I'm assuming it has something to do with heroku since that's the only difference, but I'm not very familiar with this type of error.
Also - if it helps, I can hit both urls in the browser fine, locally, and on the heroku app.
request('http://localhost:8080/api/pictures', function (error, response, body) {
// this throws error: "connect ECONNREFUSED"
});
request('http://myapp.herokuapp.com/api/pictures', function (error, response, body) {
// this works
});
connect ECONNREFUSED: the two things you want to look at first:
Is the service you're trying to connect to enabled and started?
Is there are firewall between the client and the service host/service port?
In your case:
1) I assume you've started Heroku locally (on a Windows PC?)
2) I also assume it's running on port 8080 (per your notes)
3) Please check your local firewall software, to make sure Heroku and/or port 8080 are enabled. Do you have any antivirus programs (which might introduce their own firewalls)?
4) Also, please look at these links:
http://windows.microsoft.com/en-us/windows/communicate-through-windows-firewall#1TC=windows-7
http://windows.microsoft.com/en-us/windows/open-port-windows-firewall#1TC=windows-7
https://github.com/heroku/heroku/issues/1046
http://www.debian-administration.org/article/120/Application_level_firewalling

Websocket server running fine but cannot connect from client (what url should I use?)

OK this is very simple to anyone who's used websocket and nodejs.
I have created a websocket server named ws_server.js and put it in C:\Program Files (x86)\nodejs where I have installed the nodejs framework. I started the server and it is running and it says it's listening on port 8080. So far so good, I have the server running.
Now I simply want to connect to it from client code so that I can do all that lovely stuff about capturing events using event listeners etc. The problem is, embarassingly, I cannot figure out what URL to use to connect to my websocket server.
function init() {
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket("ws://localhost:8080/"); // WHAT URL SHOULD BE USED HERE?
websocket.onopen = function(evt) { alert("OPEN") };
websocket.onclose = function(evt) { alert("CLOSE") };
websocket.onmessage = function(evt) { alert("MESSAGE") };
websocket.onerror = function(evt) { alert("ERROR") };
}
function doSend(message) {
// this would be called by user pressing a button somewhere
websocket.send(message);
alert("SENT");
}
window.addEventListener("load", init, false);
When I use ws://localhost:8080 the only events that trigger are CLOSE and ERROR. I cannot get the client to connect. I must be missing something very simple. Do I need to set up my nodejs folder in IIS for example and then use that as the URL?
Just to reiterate, the websocket server is running fine, I just don't know what URL to use to connect to it from the client.
EDIT: The websocket server reports the following error.
Specified protocol was not requested by the client.
I think I have got it working by doing the following.
var websocket = new WebSocket("ws://localhost:8080/","echo-protocol");
The problem being that I needed to specify a protocol. At least now I get the onopen event. ...if nothing much else
I was seeing the same error, the entire web server goes down. Adding the protocol fixes it but leaves me wondering why it was implemented this way. I mean, one bad request should not bring down your server.
You definitely have to encase it a try/catch, but the example code provided here https://www.npmjs.com/package/websocket (2019-08-07) does not. This issue can be easily avoided.
I just wanted to share a crazy issue that I had. I was able to connect to a websocket of an old version of a 3rd party app in one computer, but not to a newer version of the app in another.
Moreever, even in new computer with the new version of the app, The app was able to connect to the websocket, but no matter what I did, when I tried to connect with my own code, I kept getting the error message that the websocket connection failed
Long story short, They changed an apache configuration that allowed connecting to the websocket via a proxy.
In the old version, apache config was:
ProxyPass /socket/ ws://localhost:33015/ retry=10
ProxyPass /socket ws://localhost:33015/ retry=10
In the new version, apache config was changed to:
ProxyPass /socket/ ws://localhost:33015/ retry=10
By bad luck, I was trying to connect to ws://localhost/socket and not to ws://localhost/socket/. As a result, proxy was not found, and connection returned an error.
Moral of the story: Make sure that you are trying to connect to a websocket url that exists.
For me, the solution was to change the URL from ws:// to wss://. This is because the server I was connecting to had updated its security, and now only accepted wss.

Resources