Nodejs net does not throw error when trying to connect to a host thats not listening - node.js

I'm trying to test whether a remote host is listening or not using Node.js net module:
var net = require('net')
var client = net.connect({port:3000, host:remoteHostIP},function(){
});
client.on('error', function(err){
console.log("Error: "+err.message);
});
I would expect that net.connect would throw an error if it can't connect but that's not the case.
Also client.on('error') does not throw an error.
How can I check if the connection has been possible?

It will throw an error when the connection times out which is about one minute of no data transfer.
Error: connect ETIMEDOUT <ip>:3000
Use client.setTimeout() to fire a callback if there's no activity within the allocated time.

The code posted in the question works fine:
node app.js
Error: connect ECONNREFUSED
The issue has been caused by a Virtualbox VM for Docker (running on the developer machine) which had portforwarding configured for port 3000 and grabbed the net connect request for localhost.

Related

Socket.io, connect to socket inside docker container

My app is using websocket with Socket.io library. On the fronted, I open a connection to a backend socket like this :
io.connect(window.location.origin)
Everything is working fine when my backend is running on my computer. But when I run it inside a docker container, I get this error on my client:
WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket' failed: Invalid frame header
Figured out that I must make the socket listen on '0.0.0.0'

Having trouble setting up Postgres server to accept SSL connections

I'm on a Mac using Postgres.app to run a Postgres server.
I'm connecting to the server in Node.js (code copied from Heroku docs):
pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL || 'postgres://localhost:5432/my-project', function(err, client) {
if (err) throw err;
console.log('Connected to postgres! Getting schemas...');
client
.query('SELECT table_schema,table_name FROM information_schema.tables;')
.on('row', function(row) {
console.log(JSON.stringify(row));
});
});
I then followed the instructions here to allow my Postgres server to accept SSL connections. I changed the ssl setting to on in my postgresql.conf file. I also generated the required files, server.key and server.crt.
However, when I run my Node server, I get this error:
Error: The server does not support SSL connections
I ran psql and did show ssl. It returned off. So then I thought that maybe I had the wrong config file...but then I did show config_file and I'm definitely in the right place. What else am I missing?
Very likely you forgot to restart the PostgreSQL server.
In case of problems, set log_connections = on and check the PostgreSQL server log.

Run first Nodejs app on cpanel godaddy hosting

I'm trying to run my first Node.js application, but I'm having trouble. This could be an error with the firewall on CPanel, but I'm not sure.
I'm running Node.js version 5.0.0
And this is my js:
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Open Serverrn');
socket.pipe(socket); });
server.listen(674, 'my.ip.add.ress');
console.log('Server running at http://my.ip.add.ress:674/');
And final : this is my notification :
node test.js
Error is:
Server running at http://my.ip.add.ress:674/
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES my.ip.add.ress:674
at Object.exports._errnoException (util.js:860:11)
at exports._exceptionWithHostPort (util.js:883:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at doNTCallback3 (node.js:461:9)
at process._tickCallback (node.js:367:17)
at Function.Module.runMain (module.js:459:11)
at startup (node.js:136:18)
at node.js:972:3
When I open my console with port 674, it's always loading and timing out after a few seconds. Why is that? Plz help me this issue.
I do not believe GoDaddy has Node.js support, as per this: How to install nodejs application in Godaddy server
Common hosts for Node apps would be:
Digital Ocean, Nodejitsu (owned by GoDaddy anyway), Modulus, Heroku, Joyent, and AWS, I believe.
You need to use port higher than 1024. To bind lower ports you need root privileges or allow program to bind via setcap - can't be implimented at GoDaddy or other shared hosting.

I keep getting EADDRINUSE when deploying a simple node server

okay this is really strange , i am getting EADDRINUSE when implementing a simple server ,
i have changed the ports
looked for the already running node js process
tried using killall -9 node with root
looking for process hoarding the port through sudo netstat -alpn |grep node
var http= require('http');
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen("162.x.x.38",8010);
process.stdin.resume();
process.on('uncaughtException',function(err){
console.log("closing due to uncaught exception:"+err.message+" "+err.data);
server.close();
process.exit(1);
});
process.on('exit',function(){
console.log("exiting the program");
server.close();
process.exit(0);
});
process.on('SIGINT', function(){
console.log("caught cntrl +c closing the program and performing cleanups");
server.close();
process.exit(0);
});
still no help , i am running centos 6.7 on a vps hosted by bluehost , so let me sum is down, there is no node process running , no prcocess is hoarding the port
strangely enough when i put my host as localhost it starts working but still i am unable to connect it via wget
wget localhost:8010
--2015-05-22 15:19:15-- http://localhost:8010/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:8010... failed: Connection refused.
Connecting to localhost|127.0.0.1|:8010... failed: Connection refused.
when running as local host netstat gives the following :
sudo netstat -alp |grep node
unix 2 [ ACC ] STREAM LISTENING 453187 23439/node localhost
protocol as stated by netstat command is unix instead of tcp ,though i dont have much experience with linux so ....but its strange because above code is running fine in windows , but i dont know what is happening here ..help please !!
The parameters for http.listen() were backwards.
From the node.js docs:
server.listen(port[, hostname][, backlog][, callback])# Begin
accepting connections on the specified port and hostname. If the
hostname is omitted, the server will accept connections directed to
any IPv4 address (INADDR_ANY).
The correct call, from the docs link above, is as follows:
Simple, works on any address for the host
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010);
Bind to localhost only
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "127.0.0.1");
Bind to 1.2.3.4 is similar
var server=http.createServer(function(request,response){
response.end("kameha kameha....");
}).listen(8010, "1.2.3.4");

socket.io redis store on openshift

I'm trying to set up socket.io on node.js to use redisstore so i can comunicate with pubsub with multiple node on the opeshift platform, but i can't manage to connect to the redis server. I'm using this cartridge.
I tried to connect with
var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT,
process.env.OPENSHIFT_REDIS_DB_HOST);
but it doesn't work (and I found out why: createClient() only accept IP addresses) and it fallback to the default port and host, then I ran rhc port-forward:
$ rhc port-forward appname
Checking available ports ... done
Forwarding ports ...
Address already in use - bind(2) while forwarding port 8080. Trying local port 8081
To connect to a service running on OpenShift, use the Local address
Service Local OpenShift
--------------- --------------- ---- ----------------------------------------------
haproxy 127.0.0.1:8080 => 127.5.149.130:8080
haproxy 127.0.0.1:8081 => 127.5.149.131:8080
s_redis_db_host 127.0.0.1:54151 => blabla.appname.rhcloud.com:54151
Press CTRL-C to terminate port forwarding
So I tought I was doing all wrong and I had to set just the port like this:
var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT);
but all I get is this
info: socket.io started
events.js:72
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:54151 failed - connect ECONNREFUSED
at RedisClient.on_error (/var/lib/openshift/532c3790e0b8cd9bb000006b/app-root/runtime/repo/node_modules/socket.io/node_modules/redis/index.js:149:24)
at Socket.<anonymous> (/var/lib/openshift/532c3790e0b8cd9bb000006b/app-root/runtime/repo/node_modules/socket.io/node_modules/redis/index.js:83:14)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:426:14
at process._tickCallback (node.js:415:13)
DEBUG: Program node server.js exited with code 8
I tried to connect via
telnet $OPENSHIFT_REDIS_DB_HOST $OPENSHIFT_REDIS_DB_PORT
And it works fine... Do you have any suggestions? Am I doing it wrong? (I'm still new to redis and socket.io)
(I omitted the rest of the code 'cause I know it works, I have no problem on my local machine, I just can't get the connection...)
Thanks a lot
but it doesn't work (and I found out why: createClient() only accept IP addresses) and it fallback to the default port and host
It does support Host, createClient uses net.createConnection(port, host); that does support hostname.
The following code will help you find the issue:
console.log(process.env);
var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT,
process.env.OPENSHIFT_REDIS_DB_HOST, {auth_pass: process.env.OPENSHIFT_REDIS_DB_PASSWORD});
pub.on('error', console.log.bind(console));
pub.on('ready', console.log.bind(console, 'redis ready'));
Does your openshift Redis instance requires AUTH ?
don't know if it is about a recent change on openshift, but i think the problem is in the variables. Although they work for telnet.
you can try this
var redisHost = process.env.OPENSHIFT_REDIS_HOST;
var redisPort = process.env.OPENSHIFT_REDIS_PORT;
var redisPass = process.env.REDIS_PASSWORD;
var client = redis.createClient( redisPort, redisHost );
client.auth( redisPass );

Resources