Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js server only listening on ipv6

I am running a node.js server on port 5403. I can telent to the private ip on this port but cannot telnet to the public ip on the same port.

I assume the cause of this is because node.js is only listening on ipv6. This is the result of

netstat -tpln

(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.1:6379          0.0.0.0:*               LISTEN      
-
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      
-
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      
-
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      
-
tcp6       0      0 :::5611                 :::*                    LISTEN      
25715/node
tcp6       0      0 :::22                   :::*                    LISTEN      
-
tcp6       0      0 ::1:631                 :::*                    LISTEN      
-
tcp6       0      0 :::5403                 :::*                    LISTEN      
25709/node

How do I make the node server listen on ipv4

like image 491
codeyard Avatar asked Dec 13 '17 15:12

codeyard


People also ask

How do I permanently run node js server?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.

How do I stop nodeJS from listening?

close() prevents new connections and waits until all the clients are closed. To forcibly kill a node server you need to call server. close() and then close all the open connections from the server end.

Which of the following method is used to create instance of HTTP module?

Node.js HTTP module Methods: It is used to return a new instance of the HTTP server class. It is used to make an HTTP request to the server.


1 Answers

You need to specify an IPV4 address when you call the listen(), I had the same issue with the http module. If I use this:

var http = require('http');

var server = http.createServer(function(request, response) {
...
});

server.listen(13882, function() { });

It only listen on IPV6, as you can see from netstat output:

$ netstat -lntp
Proto  Recv-Q  Send-Q  Local Address  Foreign Address  State
tcp6        0       0  :::13882       :::*             LISTEN

However, if I specify an IPV4 address like this:

var http = require('http');

var server = http.createServer(function(request, response) {
...
});

server.listen(13882, "0.0.0.0", function() { });

netstat will report the server as listening on IPV4:

$ netstat -lntp
Proto  Recv-Q  Send-Q  Local Address     Foreign Address  State
tcp         0       0  0 0.0.0.0:13882   0 0.0.0.0:13882  LISTEN

I'm using Ubuntu 16.04 and npm 5.3.0.

HTH

like image 129
Dario Fiumicello Avatar answered Sep 21 '22 00:09

Dario Fiumicello