I am getting `getAddrInfo ENOTFOUND localhost` on running simple node server even though localhost is present in /etc/hosts (MacOS El Capitan) - node.js

I have a very weird thing happening while setting up and running a normal node js server.
When I run node server.js, I get following error:
Server running at http://localhost:1337/
events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND localhost
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
When I open http://localhost:1337/ in my browser, it says page does not exist.
When I run sudo node server.js, it runs fine without any error and I can access my site at https://localhost:1337
Here is my /etc/hosts file:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
My server.js:
// Require what we need
var http = require("http");
// Build the server
var app = http.createServer(function(request, response {
response.writeHead(200, {
"Content-Type": "text/plain"
});
response.end("Hello world!\n");
});
// Start that server, baby
app.listen(1337, "localhost");
console.log("Server running at http://localhost:1337/");
Other Details
OS: Mac OS (El Capitan)
Node JS version: 6.10.3 (installed globally)
NPM version: 4.5.0 (installed globally)
Following are my questions
Why is the above server.js file working with admin access but not as a normal user?
Does globally installed node only work with admin access?
Is there anything else in Mac OS which can affect DNS lookup other than /etc/hosts? I have already flushed my DNS cache and reboot my OS just to make sure that older DNS lookup is not being used.

I have fixed this same error by editing the file /etc/hosts
Added the lines:
##
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost

Related

listen EADDRINUSE: address already in use -> No matter what port I use?

NodeJS version: LTS 12.17 installed like this:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs
I have a pretty weird issue:
One of my NodeJS app suddenly reports: listen EADDRINUSE: address already in use and it doesn't help anything no matter what port # I try to change it to?
The full trace-stack looks like this:
Error: listen EADDRINUSE: address already in use 0.0.0.0:6080
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at doListen (net.js:1498:7)
at processTicksAndRejections (internal/process/task_queues.js:85:21)
I'm listening like this (_wl = a Winston logger instance):
> _server.listen(_port, '0.0.0.0', function () { _wl.info('SERVER STARTED! (listening on port # ' + _port + ')')});
I have just setup this AWS EC2 Ubuntu 20.04 instance - so I guess somehow it has something to do with this. It has been working for years on a lot of different instance earlier Windows (I guess I have never run it on Ubuntu earlier).
I execute like like:
1. cd into folder
2. node ./server.js (I have also tried to use Sudo)
Can it have something to do with permissions?
I have tried to allow all in /out going traffic in the server atteched Security Group.
I have check that the firewall in Ubuntu 20.04 is disabled as well.
Also, on the same server I'm running a Python app which exposes a web socket server and the NodeJS app has no issue to subscribe to this connection...
And yes - I have tried to check all ports in use at the server and only few ports are in use.
The NodeJS app also try to expose a websocket server at a given port - but no matter what port I try to use I get the error above.
Sorry!
I use express in my NodeJS apps and most of my apps contain the following line:
app.listen(port); // In this case port # 6080
Later on in the current app (in which I experience the issue described above) I initialize a web socket server as well like this:
const _server = require('http').createServer();
_server.on('request', app);
_server.listen(_port, '0.0.0.0', function () { _wl.info('WEB SOCKET SERVER STARTED! (listening on port # ' + _port + ')')});
in the code above both port and _port used the same ENV variable/setting(6080). Do to my app/Express in this case actually didn't use the app.listen(port) to anything and that Windows just choose to overrule the port's use-case when the web socket server afterwards got initialized with the same port number --> is the reason why I never have experienced any issues on Windows. But Linux/Ubuntu is more sensitive in this regard.
Cheers! :-)

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.

EADDRNOTAVAIL when Dockerizing Node.js app

With the following app, I am able to start it manually via npm install / node app.js. The issue is with trying to run the app via a Docker container.
Apart from what the rest of the app is (which doesn't matter because running the Docker container doesn't even get that far), the Dockerfile pulls the code from GitHub, switches to that directory, then runs app.js (which pulls the host and port from a config.json file). Attempting to run the container with the IP address of the server (what it should be listening on) results in the following error:
uncaughtException: Error: listen EADDRNOTAVAIL
Error: listen EADDRNOTAVAIL
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1024:19)
at listen (net.js:1065:10)
at net.js:1147:9
at dns.js:72:18
at process._tickCallback (node.js:442:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:929:3
Changing the port does nothing to resolve the issue. I've included the relevant files below (with certain sections replaced with {pseudocode}). Any help would be immensely appreciated, because I'm absolutely hopeless when it comes to Docker.
Dockerfile:
FROM centos:centos6
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
RUN yum install -y npm git
RUN git clone {repo.git}
COPY . /src
RUN npm install
CMD ["node", "app.js"]
config.json:
{
"app_host": "{IP of server}",
"app_port": "20000"
}
app.js:
var server = app.listen(config_json.app_port, config_json.app_host, function () {
var host = server.address().address;
var port = server.address().port;
console.log('\n listening at http://%s:%s', host, port);
});
module.exports = app;
Turns out robertklep in the comments to my question was correct: the issue was trying to explicitly pass the IP of the server into app.js. That's the way the app was configured to work just by itself with Node, but that can't be done with Docker.
The only code change needed was removing config_json.app_host from app.listen in app.js. Then, running the container by binding the exposed port to any available port on the server causes it to work.

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

Nodejs : Error get/response localhost

I'm running a Cakephp app with MAMP on localhost at port 80. I've also Node.js who listen/write on port 4000 with a js script from the Cake app (localhost/client).
When I call Node from js app, it works.
But, when I try to call a Cakephp page (localhost/saveData/123) from Node by http.get(); oder http.request for saving data in database, it crash.
events.js:71
throw arguments[1]; // Unhandled 'error' event
^ Error: connect ECONNREFUSED
at errnoException (net.js:770:11)
at Object.afterConnect [as oncomplete] (net.js:761:19)
Oder
problem with request: connect ECONNREFUSED
I've test width www.google.com, it works. It seems that's the problem is about "localhost".
Could someone help me? :)
EDIT : I've try with 127.0.0.1 too but it doesn't work (for MAMP and Node).
PS : sorry for my english, i'm french :)
Probably you have a local webserver running on port 80. Also programs like Skype can be connected to the port 80.
You can try close Skype and/or Webserver (XAMPP, WAMPP, usbwebserver, etc...).
Listing ports in Cmd.exe
$ netstat -a

Resources