-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.js
57 lines (51 loc) · 1.52 KB
/
server.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require('express');
const cp = require('child_process');
const next = require('next');
const os = require('os');
function getIPAdress() {
const interfaces = os.networkInterfaces();
const keys = Object.keys(interfaces);
// eslint-disable-next-line no-restricted-syntax
for (const devName of keys) {
const iface = interfaces[devName];
for (let i = 0; i < iface.length; i += 1) {
const alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
}
const isDev = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
const port = isDev ? 5000 : (process.env.PORT || 5000);
const app = next({ dev: isDev });
const handle = app.getRequestHandler();
app.prepare()
.then(() => {
const server = express();
server.get('*', (req, res) => handle(req, res));
server.listen(port, (err) => {
if (err) throw err;
const serverUrl = `http://localhost:${port}`;
console.log(`
App is running at:
- Local: ${serverUrl}
- Network: http://${getIPAdress()}:${port}
`);
// development auto open browser
if (isDev) {
switch (process.platform) {
// macos
case 'darwin':
cp.exec(`open ${serverUrl}`);
break;
// windows
case 'win32':
cp.exec(`start ${serverUrl}`);
break;
default:
cp.exec(`open ${serverUrl}`);
}
}
});
});