node.js server.listen on a specific IP address EADDRNOTAVAIL - node.js

When I try to listen on a specific IP address, I am getting an error.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, "10.211.56.1");
console.log('Server running at http://10.211.56.1:8080/');
I get the following error:
➜ node-test sudo node server.js
Server running at http://10.211.56.1:8080/
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1139:19)
at listen (net.js:1182:10)
at net.js:1280:9
at dns.js:85:18
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3

This basically means the ip/port combo you are using is not available to your server. This could be because the port is already in use, or, that ip address isn't one your server is using.
You should instead be either using localhost:someport or 0.0.0.0:someport, where someport is a port that isn't currently being used.

Related

Cloud 9 Node js. Can't connect to process.env.Port anymore. EADDRINUSE Error

Have tried do work out this simple code in cloud9
but it seems like some process is already located
on process.env.PORT and I can't seem to find it.
Have tried killing nodes searching for nodes.
And i'm not entirely sure how to connect to port like 3000 on cloud9. (localhost:3000)
Debugger listening on port 15454
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::8080
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1234:14)
at listen (net.js:1270:10)
at Server.listen (net.js:1366:5)
at Object.<anonymous> (/home/ubuntu/workspace/script.js:5:4)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
The code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT);

How to set up a nodejs application server in ubuntu

I don't understand why i have this problem which i show the output in the second part. While following the tutorial "how To Set Up a Node.js Application for Production on Ubuntu 14.04" i did everything.
I created a script to test my private ip address like this
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'myPrivateIp');
console.log('Server running at http://myPrivateIp:8080/');
The problem that i encountered while testing the apllication.
JoeDoe#myUbuntu:~$ node hello.js
Server running at http://myPrivateIp:8080/
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL myPrivateIp:8080
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at Server._listen2 (net.js:1218:19)
at listen (net.js:1267:10)
at net.js:1376:9
at doNTCallback3 (node.js:440:9)
at process._tickCallback (node.js:346:17)
at Function.Module.runMain (module.js:477:11)
at startup (node.js:117:18)
at node.js:951:3
The second argument of listen is not the domain name, is the address to listen on. It could be something like 192.168.x.x or 127.0.0.1 or 0.0.0.0 ( means all address. ). The default is 127.0.0.1.
Which means you should remove the second argument of listen.

running basic http server using node.js on Mac

I'm new to node.js and was trying to run the following training example for setting up a basic http server on my mac (OS X Yosemite 10.10.3):
var http = require("http");
http.createServer(function (request, response) {
request.on("end", function () {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
});
}).listen(8080);
I get the following error when executing the code:
$ node http.js events.js:85
throw er; // Unhandled 'error' event
^ Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1156:14)
at listen (net.js:1182:10)
at Server.listen (net.js:1267:5)
at Object. (/Users/sumankalyan/WebstormProjects/Node-Server/http.js:25:4)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
Not sure if this is a localhost config issue or something else. Any help much appreciated.
Another application is using the port 8080. You can run
lsof -i :8080
to see what it is. You can also just try another port.

EADDRINUSE with Node.js on OpenShift

Writing a test application in Node.js running on OpenShift and it currently will not start.
This is my code:
#!/bin/env node
var http = require('http');
var ip = process.env.OPENSHIFT_NODEJS_IP;
var port = process.env.OPENSHIFT_NODEJS_PORT;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(ip, port);
console.log('Server running');
I get the error
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1003:19)
at listen (net.js:1044:10)
at Server.listen (net.js:1104:5)
at Object.<anonymous> (/var/lib/openshift/52854c6f4382ec071400051d/app-root/runtime/repo/server.js:11:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
DEBUG: Program node server.js exited with code 8
Which seems to mean I can't bind to the port. I've found various information on why this might be but none of it seems relevant - it all seems related to permissions but surely the point of process.env.OPENSHIFT_NODEJS_* is I have permission to bind to it?
It means some other process is using your port process.env.OPENSHIFT_NODEJS_PORT. EACCESS would mean permission denied, not EADDRINUSE. Try changing to unused ports like 8080. You can check used ports with :
netstat -tulpn
netstat -tulpn | grep :$OPENSHIFT_NODEJS_PORT //filter by your port

tutorial of node.js code sample fails with Error: listen EADDRINUSE

I am a beginner programmer that is trying to learn node.js using the following tutorial site
http://www.nodebeginner.org/#hello-world
I got to the point where I was trying to set up the server but got an error with the below code
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1022:14)
at listen (net.js:1044:10)
at Server.listen (net.js:1110:5)
at Object.<anonymous> (/Users/.........../server.js:7:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
any help would be appreciated
EADDRINUSE means that address is in use.
Basically, you tried to start two servers at the same time that both use port 8888. You have to stop or kill one before starting another. The other server on port 8888 could be another process running your node script, or it could be something else in the system that serves content on port 8888.
Alternatively, you can get this if you don't let the socket settle for a few seconds after terminating the old server.
A more practical answer based on this great one.
Find out what is using port 8888 with this command:
lsof -i tcp:8888
You should get something like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 86456 myName 13u IPv6 0xa6b50fb47c9c3c81 0t0 TCP *:ddi-tcp-1 (LISTEN)
Now that you know which process is in the way, KILL IT! Softly, like so:
kill -15 86456

Resources