2

currently my hosting uses a new firewall, and according to their plan, they don't allow any http connection based on ipv6 and all connections should use ipv4. I have a service using node.js and expressJs, and I also use pm2 as a process manager to run my application, my problem is that http requests failed due to using ipv6. How could I force node.js to listen to version 4 IP address on nodeJs app.

The part of my code which I listen to a port:

const app = express();
...MANY MIDDLEWARE app.use();

mongoose.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true})
    .then(result => {
        app.listen(APP_PORT);
        socketServer.listen(SOCKET_PORT, function () {
            console.log('server listening to: %j', socketServer.address())
        });

    })
    .catch(err => {
        console.log
    });

Can I use something like below with express:

var server = http.createServer(app).listen(APP_PORT, APP_IP);
Arash Rabiee
  • 1,019
  • 2
  • 16
  • 31
  • 1
    What do mean `use IPv4` via pm2?, cause nodejs servers are like any http server and the way you you reference to that server is by `IP:port`. Now IP can be Ipv4 and IPv6. You need to specify in the node.js app to listen to only IPv4 not IPv6. And pm2 is a process manager it only runs your nodejs code. You can share a relevant part of the code for more clarity. – Aritra Chakraborty Feb 08 '20 at 22:03
  • how could I specify in node.js to listen to IPv4 – Arash Rabiee Feb 08 '20 at 22:16
  • 1
    you can simply replace `app.listen(APP_PORT);` with `app.listen(APP_PORT, '0.0.0.0');` for ipv4 – Aritra Chakraborty Feb 08 '20 at 22:25
  • I tried this but this doesn't work; although, if I use http.createServer(app).listen(APP_PORT, APP_IP); this is working, but I want to use expressJs, instead of '0.0.0.0' I set My host IP – Arash Rabiee Feb 08 '20 at 22:30
  • 1
    `app.listen` is you can say a superset of `server.listen` that you are doing. It should work same. `0.0.0.0` means it will listen to any IPv4 connection. – Aritra Chakraborty Feb 08 '20 at 22:32
  • I did exactly what you said but still listen to ,"family":"IPv6" – Arash Rabiee Feb 08 '20 at 22:35
  • 1
    I see the same behaviour, I use `app.listen(APP_PORT, '0.0.0.0');` but I still get some user IPs as ipv6 from `req.ip`. Might be some middleware? – Dev Ze Apr 20 '20 at 13:29
  • 1
    EDIT: I found a useful comment to this question https://stackoverflow.com/questions/50855419/get-only-ipv4-ips-via-nodejs-express and it states that there might simply be no ipv4 if the user connects via ipv6... – Dev Ze Apr 20 '20 at 13:36

1 Answers1

3

I was curious about it too, but based on a useful link it worked like a charm : https://github.com/nodejs/node/issues/18041.

I had this block of code :

var server = http.createServer(app);
server.listen(port);

In order to listen on ipv4, I made the following change :

var server = http.createServer(app);
server.listen(port,"127.0.0.1");

Giving the try one more time the port was listening with ipv4, otherwise it is listening on ipv6 :

$ sudo netstat -lntp |grep -i 3000
tcp        0      0 127.0.0.1:3000          0.0.0.0:*               LISTEN      20501/node /home/ml
David Mulder
  • 26,123
  • 9
  • 51
  • 114
Manuel Lazo
  • 745
  • 7
  • 7