Last active
December 18, 2015 03:07
-
-
Save zackexplosion/49c5c43cfb8c9918d7cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reference http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js | |
var express = require('express'); | |
var app = express(); | |
var os = require('os'); | |
var ifaces = os.networkInterfaces(); | |
app.get('/', function (req, res) { | |
res.send('Hello World!'); | |
}); | |
var server = app.listen(3000, function () { | |
var host = server.address().address; | |
var port = server.address().port; | |
Object.keys(ifaces).forEach(function (ifname) { | |
ifaces[ifname].forEach(function (iface) { | |
if ('IPv4' !== iface.family ) { | |
// skip over non-ipv4 addresses | |
return; | |
} | |
console.log('Example app listening at', iface.address + ":" + port); | |
}); | |
}); | |
}); | |
// OUTPUT | |
/* | |
➜ test node app.js | |
Example app listening at 127.0.0.1:3000 | |
Example app listening at 192.168.1.176:3000 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment