-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathserver.js
60 lines (52 loc) · 1.48 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
58
59
const express = require('express');
const cp = require('child_process');
const next = require('next');
const os = require('os');
const { publicRuntimeConfig, serverRuntimeConfig } = require('./next.config');
function getIPAdress() {
const interfaces = os.networkInterfaces();
for (let devName in interfaces) {
const iface = interfaces[devName];
for (let i = 0; i < iface.length; i++) {
const alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
}
const { isDev } = publicRuntimeConfig;
const { PORT } = serverRuntimeConfig;
const app = next({ dev: isDev });
const handle = app.getRequestHandler();
app.prepare()
.then(() => {
const server = express();
server.get('*', (req, res) => {
return 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}`);
}
}
});
});