-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
executable file
·64 lines (54 loc) · 1.6 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
60
61
62
63
64
/*eslint-env node*/
const express = require("express");
const app = express();
const cors = require("cors");
const fs = require("fs");
const http = require("http");
const https = require("https");
const { createProxyMiddleware } = require("http-proxy-middleware");
const path = require("path");
const corsConfig = {
methods: "GET,POST,PUT,DELETE",
origin: "*",
credentials: true,
};
// Configure express.
app.use(cors(corsConfig));
app.use(
"/api/local",
createProxyMiddleware({
pathRewrite: { "^/api/[^/]+/": "/" },
target: `http://${
process.env.REACT_APP_BACKEND_HOSTNAME || "host.docker.internal"
}:${process.env.REACT_APP_BACKEND_PORT || 8091}`,
changeOrigin: true,
})
);
const buildDirPath = path.join(__dirname, "./build");
app.use(express.static(buildDirPath));
// unknown routes should go to index.html as they will be handled by the client
app.get("*", (req, res) => {
res.sendFile("index.html", { root: buildDirPath });
});
app.use((err, req, res, next) => {
if (err) {
const code = err.code || 500;
return res.status(code).json(err);
}
});
const port = process.env.PORT || 8080;
// We use `||` rather than `&&` so that if one env var is set but not the other (probably a mistake), we error out.
if (process.env.HTTPS_KEY || process.env.HTTPS_CERT) {
const tls = {
key: fs.readFileSync(process.env.HTTPS_KEY),
cert: fs.readFileSync(process.env.HTTPS_CERT),
};
https.createServer(tls, app).listen(port);
} else {
http.createServer(app).listen(port);
}
console.log(`
API server listening on port ${port}
Server PID: ${process.pid}
`);
module.exports = app;