-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathserver.js
50 lines (42 loc) · 1.31 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
const express = require('express');
const cp = require('child_process');
const next = require('next');
const { publicRuntimeConfig, serverRuntimeConfig } = require('./next.config');
const { isDev } = publicRuntimeConfig;
const { PORT } = serverRuntimeConfig;
// 判断开发环境和生产环境
const dev = isDev;
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare()
.then(() => {
const server = express();
server.get('/topicDetail/:id', (req, res) => {
const { id } = req.params;
return app.render(req, res, '/topicDetail', { id });
});
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(PORT, err => {
if (err) throw err;
const serverUrl = `http://localhost:${PORT}`;
console.log(`> Ready on ${serverUrl}`);
// 开发环境自动启动
if (dev) {
switch (process.platform) {
//mac系统使用 一下命令打开url在浏览器
case 'darwin':
cp.exec(`open ${serverUrl}`);
break;
//win系统使用 一下命令打开url在浏览器
case 'win32':
cp.exec(`start ${serverUrl}`);
break;
// 默认mac系统
default:
cp.exec(`open ${serverUrl}`);
}
}
});
});