Search code examples
node.jsserveripv6ipv4

Run same Node.js App on IPV4 and IPV6 simultaneously?


I know when I have the hostname as "::" this means the same as "0.0.0.0" in IPV4.

    const express = require('express')
    const app = express()
    const port =80
    const host6 = "::";
    const host4 = "0.0.0.0";
    
   app.get('/', (req, res) => {
      res.send('Hello World!')
    })
    
    app.listen(port, host6, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })

This App should now be accessible through IPV6 but als IPV4. But devices which don't understand IPV6 can not make the initial request over IPV6, so for them the App is not accessible.

How can I run this App on both IP versions on the same Port 80?


Solution

  • Unconfirmed, but I would try to remove the host from the arguments passed to app.listen and only pass the port, app.listen(port).

    Express docs for app.listen state that

    This method is identical to Node’s http.Server.listen().

    Node's documentation for http.Server.listen state that

    This method is identical to server.listen() from net.Server.

    Node's documentation for net.Server states that

    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.