-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (42 loc) · 1.19 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
require("dotenv").config({
path: `.env.${process.env.ENV}`,
});
const express = require("express");
const next = require("next");
const { createProxyMiddleware } = require("http-proxy-middleware");
const { ENV } = process.env;
const port = process.env.PORT || 3000;
const proxyApiUrl = process.env.PROXY_API_URL || "";
// dev - true: use source code path ,false: use bundle path
const dev = ENV !== "prod" && ENV !== "uat" && ENV !== "sit";
const app = next({ dev });
const handle = app.getRequestHandler();
app
.prepare()
.then(() => {
const server = express();
if (proxyApiUrl !== "") {
const apiPaths = {
"/api": {
target: proxyApiUrl,
pathRewrite: {
"^/api": "",
},
changeOrigin: true,
},
};
server.use("/api", createProxyMiddleware(apiPaths["/api"]));
}
server.all("*", (req, res) => {
return handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
// eslint-disable-next-line no-console
console.log(`> Ready on http://localhost:${port}`);
});
})
.catch(err => {
// eslint-disable-next-line no-console
console.log("Error:::::", err);
});