NodeJS - Error: listen EADDRNOTAVAIL: address not available - node.js

I am running a Node app that should be hosted on a local server.
At the moment, I am sending just a plaintext response.
const http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {ContentType: 'text/plain'});
res.end("test");
});
When I listen to the localhost everything works fine and I am able to send the request from my browser.
server.listen(3000, '127.0.0.1'); // works fine, on the same machine
However, if I try to listen to a port on my LAN network by typing the router's IP, I get an error.
server.listen(3000, '192.168.0.1'); // causes an error
Error: listen EADDRNOTAVAIL: address not available 192.168.0.1:3
000
at Server.setupListenHandle [as _listen2] (net.js:1253:19)
at listenInCluster (net.js:1318:12)
at doListen (net.js:1451:7)
at process._tickCallback (internal/process/next_tick.js:63:1
9)
at Function.Module.runMain (internal/modules/cjs/loader.js:7
57:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1297:8)
at process._tickCallback (internal/process/next_tick.js:63:1
9)
[... lines matching original stack trace ...]
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I have tried this with my public IP address unsuccessfully.
Is there any way to listen to a port on a LAN server so that I can send requests from any computer on the network?
Also, I would later like my application to run on any computer on any LAN network. How can I dynamically add my host?

Try this
server.listen(3000, '0.0.0.0');
you can access your node application from other machine
e.g
your lan ip is 192.168.0.101 then you can browse 192.168.0.101:3000 from other machine

I got this error, the issue was I had a VPN connected

Be careful with your IP.
Sometimes it changes. When I work with Node I have to check the ipconfig in Shell... I hope with that it could help if someone else have the same problem...

The right IP address actually turned out to be 192.168.0.104.
The IP address of a server machine should be used, not that of a router.

Related

How to use app.listen(port, host) with public hostname?

Can I put public domain name as host argument in app.listen(port, host)?
I know my ip, and I have 3 domain names on this ip. domain1.com, domain2.com all on same ip. Now, I'm using switch in app.get('/') to routing request of different domain names. However, I'm wondering is there a way to route host when setting up app.listen.
I found the entry of app.listen([port[, host[, backlog]]][, callback]) is really vague on expressjs website. There's nothing about the "host" argument. I know it can be set to localhost or 127.0.0.1. But my code failed when I put public domain name in it.
Can anybody explain how the host argument works in app.listen? Thank you.
var express = require('express');
var app = express();
app.get('/', function(req, res) {res.send("hello")});
app.listen(3000, "my.domain");
Error code:
Error: listen EADDRNOTAVAIL: address not available myip:3000
at Server.setupListenHandle [as _listen2] (net.js:1281:19)
at listenInCluster (net.js:1346:12)
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1485:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:65:10)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1325:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRNOTAVAIL',
errno: 'EADDRNOTAVAIL',
syscall: 'listen',
address: 'myip',
port: 3000
}
The server can't run.
Try to start server with hostname 0.0.0.0 and get access from other network points by your IP (check by ifconfig).
Find out the public ip of your public domain, then put it in the app.listen.
Example if your public ip is 1.2.3.4:
app.listen(3000,'1.2.3.4');

How do I access other IPv6 Adresses from my vps

Hello StackOverflow!
I recently bought a VPS, where I was given 64 IPv6 addresses, however I find myself unable to use any of them except :1. The IPv6 gateway looks like this (masked with x for security reasons) xxxx:xxxx:x:xxx::a where a is 1 to 64, I should have all of them available to my service, I'm just not able to use any of them except 1. It says that 1 is automatically assigned to the OS installed on my VPS, does that mean I have to assign the rest of the addresses as well? If so, how do I do that?
Here's the code I used in Node.js to try this.
const Express = require("express");
const App = Express();
App.use(Express.static(__dirname+"/www"));
const Listener = App.listen(1024, "xxxx:xxxx:x:x:xx::2", () => {
const addr = Listener.address();
console.log("Listening on " + addr.address + ":" + addr.port);
});
The error that I get when I'm starting it is:
Error: listen EADDRNOTAVAIL xxxxx:xxxx:x:x:xx::2:1024
at Server.setupListenHandle [as _listen2] (net.js:1318:19)
at listenInCluster (net.js:1383:12)
at doListen (net.js:1509:7)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at startup (internal/bootstrap/node.js:236:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1362:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
[... lines matching original stack trace ...]
at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)
Hello!
I managed to resolve this on my own after finding the right keywords to google for with the help of #Paul
The solution was to add each address to the interfaces file.
I used the command:
sudo ifconfig eth0 inet6 add xxxx:xxxx:x:x:xx::a/64

port 100 not running in node.js in localhost

I have created a http server in node.js with the following code, and trying to run it on port 100:
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("Howdy");
}).listen(100);
console.log("server running on port 100");
With this, the server does not start and I am getting the following error message on the linux console:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at Object.<anonymous> (/home/badhai/Desktop/mainn.js:6: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)
But if I lift a sails.js app on port 100, it runs successfully on port 100. However, the above code runs successfully on port 8081. I want to know if I need to make any changes in the server creation method or elsewhere so that it can be made to run successfully on port 100?
The EACCES part of the error message is the key here - it means you don't have access to that port. Ports < 1024 are system reserved. It's better to use ports in the range 1024-65535
Most modern operating systems limit binding to reserved ports (less than 1024) to processes running as root (or equivalent).
If you absolutely must bind to port 100, googling around will give you a bunch of ways to do it:
https://gist.github.com/firstdoit/6389682
It's better to use ports > 1024. I'm using ports starting from 3000. But if you really want to start it on port 100 and you understand what you're doing then install setcap and just allow to bind ports <1024.
> sudo apt-get install setcap
> sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/node
NodeJS can be installed in other directory also, so better to check where it is and call above setcap command with your path.
> which node
/usr/local/bin/node

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

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.

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.

Resources