How can I get the ip address of the server that meteor is running on?
You could do something like:
var os = Npm.require('os');
var getIp = function() {
// Get interfaces
var netInterfaces = os.networkInterfaces();
// Result
var result = [];
for (var id in netInterfaces) {
var netFace = netInterfaces[id];
for (var i = 0; i < netFace.length; i++) {
var ip = netFace[i];
if (ip.internal === false && ip.family === 'IPv4') {
result.push(ip);
}
}
}
return result;
};
Meteor.methods({
'server-ip': function() {
return getIp();
}
});
2 Likes
hostname is also useful:
os.hostname()
1 Like
Great, thanks raix!!
I’m using docker and os.networkInterfaces() is giving me the wrong ip. Still looking for a way to get the host’s ip.
Is it giving you the ip internal to the container?
1 Like
^ generally speaking it looks like default route should point to the host
It still works after 7 years just a little modification.
import os from 'os';
export const getServerAddresses = () => {
// Get interfaces
const netInterfaces = os.networkInterfaces();
// Result
const result = [];
Object.keys(netInterfaces).map((id) => {
const netFace = netInterfaces[id];
for (let i = 0; i < netFace.length; i += 1) {
const ip = netFace[i];
if (ip.internal === false && ip.family === 'IPv4') {
result.push(ip);
}
}
return null;
});
return result;
};
export const getServerHostName = () => os.hostname();
1 Like