Skip to content

Instantly share code, notes, and snippets.

@imesh
Created August 20, 2017 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imesh/99482c814e2fb59494e8a981fdaea64c to your computer and use it in GitHub Desktop.
Save imesh/99482c814e2fb59494e8a981fdaea64c to your computer and use it in GitHub Desktop.
const express = require('express')
const os = require('os');
const app = express()
app.get('/', function (req, res) {
res.send('Response from ' + getIPAddress())
})
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
app.listen(8080, function () {
console.log('Echo server listening on port 8080!')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment