Created
February 13, 2018 13:42
-
-
Save carljdp/d0a9f745f988ffe71910d0562ae3eabb to your computer and use it in GitHub Desktop.
Get Meteor server URL/IP when serving or testing from localhost
This file contains 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
import { Meteor } from 'meteor/meteor'; | |
import os from 'os'; | |
// If this were production env var ROOT_URL would have had a value | |
let rootUrl = Meteor.absoluteUrl(); | |
if ((Meteor.isDevelopment) && (rootUrl === 'http://localhost:3000/')) { | |
rootUrl = _getServerUrlFromOs(); | |
} | |
function _getServerUrlFromOs() { | |
// return this if all else fails | |
let url = Meteor.absoluteUrl(); | |
const osInterfaces = os.networkInterfaces(); | |
const serverPort = process.env.PORT; | |
if (osInterfaces.eth0) { | |
url = `http://${osInterfaces.eth0[0].address}:${serverPort}/`; | |
console.log(`[server isDevelopment] Overriding Meteor.absoluteUrl from eth0 to ${url}`); | |
} | |
else if (osInterfaces.wlan0) { | |
url = `http://${osInterfaces.wlan0[0].address}:${serverPort}/`; | |
console.log(`[server isDevelopment] Overriding Meteor.absoluteUrl from wlan0 to ${url}`); | |
} | |
// works only for the 1st Ethernet or WLAN interface in your system | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment