devtools.js
858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
const os = require('os');
const fs = require('fs');
let devHost;
const getFirstLocalIp = () => {
const ifaces = os.networkInterfaces();
const res = [];
Object.keys(ifaces).forEach(function(ifname) {
ifaces[ifname].forEach(function(iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
return;
}
res.push(iface.address);
});
});
return res[0] || 'localhost';
};
if (!devHost) {
fs.readFile('.devhost', (err, buf) => {
if (!err) {
devHost = JSON.parse(buf.toString()).host;
} else {
devHost = getFirstLocalIp();
}
});
}
module.exports = () => {
return (req, res, next) => {
Object.assign(res.locals, {
devHost: devHost
});
next();
};
};