Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 532
aviya.developer Avatar asked Sep 06 '25 03:09

aviya.developer


1 Answers

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}!`);
});
like image 150
aviya.developer Avatar answered Sep 07 '25 22:09

aviya.developer