Search code examples
javascriptnode.jsexpressserverip-address

How can i display the ip address exposing an express.js development server to other machines in the network?


When I start up a basic express.js server I usually console.log a message that says it is running and listening on port .

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('App reached.'));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

However, I would like to add to this message the available IP address exposing the app to other machines on the network. Somewhat like create-react-app basic template does. How can i reach that info with my javascript code?


Solution

  • Has indeed already been answered: Get local IP address in node.js

    I preferred this solution:

    npm install ip
    
    const express = require('express');
    const ip = require('ip');
    const ipAddress = ip.address();
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => res.send('App reached.'));
    
    app.listen(port, () => {
      console.log(`Example app listening on port ${port}!`);
      console.log(`Network access via: ${ipAddress}:${port}!`);
    });