t7n-localhost
Version:
get your 'loalhost'
28 lines (25 loc) • 689 B
JavaScript
const os = require('os')
const info = {
get localhost() {
let localhost
try {
var network = os.networkInterfaces()
var networkKeys = Object.keys(network)
var networkKey = ''
var networkKeyIndex = 0
var isEnStart = false
var targetKey
do {
networkKey = networkKeys[networkKeyIndex]
isEnStart = /^en\d/.test(networkKey)
if (isEnStart) targetKey = networkKey
networkKeyIndex++
} while (networkKeyIndex < networkKeys.length && !isEnStart)
localhost = network[targetKey][1].address
} catch (e) {
localhost = 'localhost';
}
return localhost
}
}
module.exports = info.localhost