-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrouter.js
46 lines (41 loc) · 1.41 KB
/
router.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 router = express();
const path = require("path");
const { createProxyMiddleware } = require("http-proxy-middleware");
const logger = require("./api/util/logger");
const adminPath = process.pkg
? path.join(path.dirname(process.execPath), "./views/admin")
: path.join(__dirname, "./views/admin");
logger.log("verbose", `ROUTER: Serving admin route to ${adminPath}`);
router.use("/admin/", express.static(adminPath));
const fePath = process.pkg
? path.join(path.dirname(process.execPath), "./views/frontend")
: path.join(__dirname, "./views/frontend");
logger.log("verbose", `ROUTER: Serving frontend route to ${fePath}`);
router.use("/", express.static(fePath));
router.use(
"/api",
createProxyMiddleware({
target: "http://localhost:7778",
headers: {
Connection: "keep-alive",
},
xfwd: true,
logProvider: function (provider) {
return logger;
},
pathRewrite: function (path, req) {
if (req.basePath !== "/") {
return path.replace("//", "/").replace(`${req.basePath}/api/`, "/");
} else {
return path.replace("/api/", "/");
}
},
})
);
logger.log("verbose", `ROUTER: API proxy setup - Proxying /api -> /`);
router.get("*", function (req, res) {
logger.log("warn", `ROUTER: Not found - ${req.path} | IP: ${req.ip}`);
res.status(404).send(`Petio Router: not found - ${req.path}`);
});
module.exports = router;