不想用循环
const path = require("path");
const os = require("os");
let localhost;
try {
const network = os.networkInterfaces();
for (let i in network) {
for (let j in network[i]) {
const val = network[i][j];
if (val.family === "IPv4" && val.address !== "127.0.0.1") {
localhost = val.address;
break;
}
}
}
} catch (e) {
localhost = "http://localhost";
}
一句话解决:
另外,我记得
family
的取值是4
或6
,没有"IPv4"
吧?