问题
Node js application in local it works fine. When tried running in server it shows error.
ubuntu@ip-172-*-*-*:~/sample_nodejs$ node main.js
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL 52.*.*.*:1122
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1350:19)
at listenInCluster (net.js:1408:12)
at doListen (net.js:1517:7)
at _combinedTickCallback (internal/process/next_tick.js:141:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
And here is my port running status. Used command netstat -tulpn
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
-
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
-
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
-
tcp6 0 0 :::22 :::* LISTEN
-
tcp6 0 0 :::80 :::* LISTEN
-
udp 0 0 127.0.0.53:53 0.0.0.0:*
-
udp 0 0 172.*.*.*:68 0.0.0.0:* -
Tried changing iptables
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
still it shows the error. Is this server fault or i need ask permission from server side?
here is my sample code i tried using public ip address:
var express = require('express');
var app = express();
const http = require('http');
const hostname = '52.*.*.*';
const port = 1122;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
回答1:
Don't try to listen on the external IP address on an EC2 instance - it isn't available. Instead, listen on the "any" interface (sometimes shown as 0.0.0.0
). According to the docs I can find this is done with server.listen without specifying a host parameter:
If
host
is omitted, the server will accept connections on the unspecified IPv6 address (::
) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0
) otherwise.In most operating systems, listening to the unspecified IPv6 address (
::
) may cause thenet.Server
to also listen on the unspecified IPv4 address (0.0.0.0
).
The port you listen on will be accessible from the outside world though you will have to expose it with a security group.
EDIT
You'll need to change your code to be:
var express = require('express');
var app = express();
const http = require('http');
const port = 1122;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, () => {
console.log(`Server running at on port ${port}`);
});
回答2:
As stundbar said, when you make a server, you mustn't set the public IP but 0.0.0.0 or localhost. Don't set 127.0.0.1 because you won't be able to access it outside the server's machine.
来源:https://stackoverflow.com/questions/53955562/node-js-error-listen-eaddrnotavail-52-1122