1

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?

aviya.developer
  • 3,343
  • 2
  • 15
  • 41
  • 1
    Possible duplicate of [Get local IP address in node.js](https://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js) – Weedoze Nov 22 '19 at 14:11

1 Answers1

2

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}!`);
});
aviya.developer
  • 3,343
  • 2
  • 15
  • 41