-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (40 loc) · 1.1 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
const express = require("express");
const next = require("next");
const { createProxyMiddleware } = require("http-proxy-middleware");
// 配置运行端口
const port = process.env.PORT || 3000;
// 判断是否为开发环境
const dev = process.env.NODE_ENV !== "production";
// 初始化 app
const app = next({ dev });
const handle = app.getRequestHandler();
// 代理配置表,这里和一般的 webpack 配置是一样的。
const proxyTable = {
"/api": {
target: "http://192.168.8.63:9002",
pathRewrite: {
"^/api": "/",
},
changeOrigin: true,
},
};
app
.prepare()
.then(() => {
const server = express();
// 如果是开发环境,则代理接口
if (dev) {
server.use("/api", createProxyMiddleware(proxyTable["/api"]));
}
// 托管所有请求
server.all("*", (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
})
.catch((err) => {
console.log("Error:::::", err);
});